Skip to content

Commit

Permalink
Shorten multiple use statements (#67)
Browse files Browse the repository at this point in the history
* Shorten multiple use statements

* cs

* Added import grouping

* Fixed style CI config

* cs

* cs

* Remove extra use statement from RequestTrait

* Properly import HTTPlug factories
  • Loading branch information
Nyholm committed Aug 2, 2018
1 parent 905e92f commit 9a896f9
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 54 deletions.
1 change: 1 addition & 0 deletions .styleci.yml
Expand Up @@ -10,3 +10,4 @@ enabled:

disabled:
- phpdoc_annotation_without_dot # This is still buggy: https://github.com/symfony/symfony/pull/19198
- single_import_per_statement
8 changes: 3 additions & 5 deletions src/Factory/HttplugFactory.php
Expand Up @@ -4,17 +4,15 @@

namespace Nyholm\Psr7\Factory;

use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\Stream;
use Nyholm\Psr7\Uri;
use Nyholm\Psr7\{Request, Response, Stream, Uri};
use Psr\Http\Message\UriInterface;
use Http\Message\{MessageFactory, StreamFactory, UriFactory};

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
* @author Martijn van der Ven <martijn@vanderven.se>
*/
final class HttplugFactory implements \Http\Message\MessageFactory, \Http\Message\StreamFactory, \Http\Message\UriFactory
final class HttplugFactory implements MessageFactory, StreamFactory, UriFactory
{
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
{
Expand Down
20 changes: 2 additions & 18 deletions src/Factory/Psr17Factory.php
Expand Up @@ -4,24 +4,8 @@

namespace Nyholm\Psr7\Factory;

use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Stream;
use Nyholm\Psr7\UploadedFile;
use Nyholm\Psr7\Uri;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use Nyholm\Psr7\{Request, Response, ServerRequest, Stream, UploadedFile, Uri};
use Psr\Http\Message\{RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, ServerRequestFactoryInterface, ServerRequestInterface, StreamFactoryInterface, StreamInterface, UploadedFileFactoryInterface, UploadedFileInterface, UriFactoryInterface, UriInterface};

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand Down
4 changes: 1 addition & 3 deletions src/Request.php
Expand Up @@ -4,9 +4,7 @@

namespace Nyholm\Psr7;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Message\{RequestInterface, StreamInterface, UriInterface};

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
Expand Down
3 changes: 1 addition & 2 deletions src/RequestTrait.php
Expand Up @@ -4,7 +4,6 @@

namespace Nyholm\Psr7;

use InvalidArgumentException;
use Psr\Http\Message\UriInterface;

/**
Expand Down Expand Up @@ -44,7 +43,7 @@ public function getRequestTarget(): string
public function withRequestTarget($requestTarget): self
{
if (preg_match('#\s#', $requestTarget)) {
throw new InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
}

$new = clone $this;
Expand Down
4 changes: 1 addition & 3 deletions src/Response.php
Expand Up @@ -4,8 +4,7 @@

namespace Nyholm\Psr7;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\{ResponseInterface, StreamInterface};

/**
* @author Michael Dowling and contributors to guzzlehttp/psr7
Expand Down Expand Up @@ -41,7 +40,6 @@ final class Response implements ResponseInterface
public function __construct(int $status = 200, array $headers = [], $body = null, string $version = '1.1', string $reason = null)
{
$this->statusCode = (int) $status;

if ('' !== $body && null !== $body) {
$this->stream = Stream::create($body);
}
Expand Down
5 changes: 1 addition & 4 deletions src/ServerRequest.php
Expand Up @@ -4,10 +4,7 @@

namespace Nyholm\Psr7;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Message\{ServerRequestInterface, StreamInterface, UploadedFileInterface, UriInterface};

/**
* @author Michael Dowling and contributors to guzzlehttp/psr7
Expand Down
33 changes: 14 additions & 19 deletions src/UploadedFile.php
Expand Up @@ -4,10 +4,7 @@

namespace Nyholm\Psr7;

use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use RuntimeException;
use Psr\Http\Message\{StreamInterface, UploadedFileInterface};

/**
* @author Michael Dowling and contributors to guzzlehttp/psr7
Expand Down Expand Up @@ -67,7 +64,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename
*
* @param string|resource|StreamInterface $streamOrFile
*
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
private function setStreamOrFile($streamOrFile): void
{
Expand All @@ -78,18 +75,18 @@ private function setStreamOrFile($streamOrFile): void
} elseif ($streamOrFile instanceof StreamInterface) {
$this->stream = $streamOrFile;
} else {
throw new InvalidArgumentException('Invalid stream or file provided for UploadedFile');
throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile');
}
}

private function setError($error): void
{
if (false === is_int($error)) {
throw new InvalidArgumentException('Upload file error status must be an integer');
throw new \InvalidArgumentException('Upload file error status must be an integer');
}

if (false === in_array($error, self::$errors)) {
throw new InvalidArgumentException('Invalid error status for UploadedFile');
throw new \InvalidArgumentException('Invalid error status for UploadedFile');
}

$this->error = $error;
Expand All @@ -98,7 +95,7 @@ private function setError($error): void
private function setSize($size): void
{
if (false === is_int($size)) {
throw new InvalidArgumentException('Upload file size must be an integer');
throw new \InvalidArgumentException('Upload file size must be an integer');
}

$this->size = $size;
Expand All @@ -117,7 +114,7 @@ private function isStringNotEmpty($param): bool
private function setClientFilename($clientFilename): void
{
if (false === $this->isStringOrNull($clientFilename)) {
throw new InvalidArgumentException('Upload file client filename must be a string or null');
throw new \InvalidArgumentException('Upload file client filename must be a string or null');
}

$this->clientFilename = $clientFilename;
Expand All @@ -126,7 +123,7 @@ private function setClientFilename($clientFilename): void
private function setClientMediaType($clientMediaType): void
{
if (false === $this->isStringOrNull($clientMediaType)) {
throw new InvalidArgumentException('Upload file client media type must be a string or null');
throw new \InvalidArgumentException('Upload file client media type must be a string or null');
}

$this->clientMediaType = $clientMediaType;
Expand All @@ -141,16 +138,16 @@ private function isOk(): bool
}

/**
* @throws RuntimeException if is moved or not ok
* @throws \RuntimeException if is moved or not ok
*/
private function validateActive(): void
{
if (false === $this->isOk()) {
throw new RuntimeException('Cannot retrieve stream due to upload error');
throw new \RuntimeException('Cannot retrieve stream due to upload error');
}

if ($this->moved) {
throw new RuntimeException('Cannot retrieve stream after it has already been moved');
throw new \RuntimeException('Cannot retrieve stream after it has already been moved');
}
}

Expand All @@ -172,13 +169,11 @@ public function moveTo($targetPath): void
$this->validateActive();

if (false === $this->isStringNotEmpty($targetPath)) {
throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
throw new \InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
}

if (null !== $this->file) {
$this->moved = 'cli' === php_sapi_name()
? rename($this->file, $targetPath)
: move_uploaded_file($this->file, $targetPath);
$this->moved = 'cli' === php_sapi_name() ? rename($this->file, $targetPath) : move_uploaded_file($this->file, $targetPath);
} else {
$stream = $this->getStream();
if ($stream->isSeekable()) {
Expand All @@ -189,7 +184,7 @@ public function moveTo($targetPath): void
}

if (false === $this->moved) {
throw new RuntimeException(sprintf('Uploaded file could not be moved to %s', $targetPath));
throw new \RuntimeException(sprintf('Uploaded file could not be moved to %s', $targetPath));
}
}

Expand Down

0 comments on commit 9a896f9

Please sign in to comment.