Skip to content

Commit

Permalink
[CS] native_function_invocation (#235)
Browse files Browse the repository at this point in the history
* [CS] native_function_invocation

* fixed psalm baseline
  • Loading branch information
Nyholm committed Apr 16, 2023
1 parent 8793ff5 commit 52887ca
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 103 deletions.
3 changes: 1 addition & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
return $config->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => array('syntax' => 'short'),
'native_function_invocation' => true,
'native_function_invocation' => ['include'=> ['@all']],
'native_constant_invocation' => true,
'ordered_imports' => true,
'declare_strict_types' => false,
Expand Down
2 changes: 1 addition & 1 deletion psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/Stream.php">
<NoValue occurrences="1">
<code>return trigger_error((string) $e, \E_USER_ERROR);</code>
<code>return \trigger_error((string) $e, \E_USER_ERROR);</code>
</NoValue>
</file>
</files>
6 changes: 3 additions & 3 deletions src/Factory/Psr17Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function createStreamFromFile(string $filename, string $mode = 'r'): Stre
throw new \RuntimeException('Path cannot be empty');
}

if (false === $resource = @fopen($filename, $mode)) {
if (false === $resource = @\fopen($filename, $mode)) {
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
throw new \InvalidArgumentException(sprintf('The mode "%s" is invalid.', $mode));
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
}

throw new \RuntimeException(sprintf('The file "%s" cannot be opened: %s', $filename, error_get_last()['message'] ?? ''));
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? ''));
}

return Stream::create($resource);
Expand Down
24 changes: 12 additions & 12 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getHeaders(): array

public function hasHeader($header): bool
{
return isset($this->headerNames[strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')]);
return isset($this->headerNames[\strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')]);
}

public function getHeader($header): array
Expand All @@ -66,7 +66,7 @@ public function getHeader($header): array
throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string');
}

$header = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
$header = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
if (!isset($this->headerNames[$header])) {
return [];
}
Expand All @@ -78,13 +78,13 @@ public function getHeader($header): array

public function getHeaderLine($header): string
{
return implode(', ', $this->getHeader($header));
return \implode(', ', $this->getHeader($header));
}

public function withHeader($header, $value): self
{
$value = $this->validateAndTrimHeader($header, $value);
$normalized = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
$normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');

$new = clone $this;
if (isset($new->headerNames[$normalized])) {
Expand Down Expand Up @@ -114,7 +114,7 @@ public function withoutHeader($header): self
throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string');
}

$normalized = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
$normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
if (!isset($this->headerNames[$normalized])) {
return $this;
}
Expand Down Expand Up @@ -156,10 +156,10 @@ private function setHeaders(array $headers): void
$header = (string) $header;
}
$value = $this->validateAndTrimHeader($header, $value);
$normalized = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
$normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
if (isset($this->headerNames[$normalized])) {
$header = $this->headerNames[$normalized];
$this->headers[$header] = array_merge($this->headers[$header], $value);
$this->headers[$header] = \array_merge($this->headers[$header], $value);
} else {
$this->headerNames[$normalized] = $header;
$this->headers[$header] = $value;
Expand Down Expand Up @@ -187,17 +187,17 @@ private function setHeaders(array $headers): void
*/
private function validateAndTrimHeader($header, $values): array
{
if (!\is_string($header) || 1 !== preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $header)) {
if (!\is_string($header) || 1 !== \preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $header)) {
throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string');
}

if (!\is_array($values)) {
// This is simple, just one value.
if ((!is_numeric($values) && !\is_string($values)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $values)) {
if ((!\is_numeric($values) && !\is_string($values)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $values)) {
throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings');
}

return [trim((string) $values, " \t")];
return [\trim((string) $values, " \t")];
}

if (empty($values)) {
Expand All @@ -207,11 +207,11 @@ private function validateAndTrimHeader($header, $values): array
// Assert Non empty array
$returnValues = [];
foreach ($values as $v) {
if ((!is_numeric($v) && !\is_string($v)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $v)) {
if ((!\is_numeric($v) && !\is_string($v)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $v)) {
throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings');
}

$returnValues[] = trim((string) $v, " \t");
$returnValues[] = \trim((string) $v, " \t");
}

return $returnValues;
Expand Down
2 changes: 1 addition & 1 deletion src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function withRequestTarget($requestTarget): self
throw new \InvalidArgumentException('Request target must be a string');
}

if (preg_match('#\s#', $requestTarget)) {
if (\preg_match('#\s#', $requestTarget)) {
throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function withStatus($code, $reasonPhrase = ''): self

$code = (int) $code;
if ($code < 100 || $code > 599) {
throw new \InvalidArgumentException(sprintf('Status code has to be an integer between 100 and 599. A status code of %d was given', $code));
throw new \InvalidArgumentException(\sprintf('Status code has to be an integer between 100 and 599. A status code of %d was given', $code));
}

$new = clone $this;
Expand Down
2 changes: 1 addition & 1 deletion src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(string $method, $uri, array $headers = [], $body = n
$this->uri = $uri;
$this->setHeaders($headers);
$this->protocol = $version;
parse_str($uri->getQuery(), $this->queryParams);
\parse_str($uri->getQuery(), $this->queryParams);

if (!$this->hasHeader('Host')) {
$this->updateHostFromUri();
Expand Down
46 changes: 23 additions & 23 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function __construct($body)
}

$this->stream = $body;
$meta = stream_get_meta_data($this->stream);
$this->seekable = $meta['seekable'] && 0 === fseek($this->stream, 0, \SEEK_CUR);
$meta = \stream_get_meta_data($this->stream);
$this->seekable = $meta['seekable'] && 0 === \fseek($this->stream, 0, \SEEK_CUR);
$this->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]);
$this->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]);
}
Expand All @@ -81,9 +81,9 @@ public static function create($body = ''): StreamInterface
}

if (\is_string($body)) {
$resource = fopen('php://temp', 'rw+');
fwrite($resource, $body);
fseek($resource, 0);
$resource = \fopen('php://temp', 'rw+');
\fwrite($resource, $body);
\fseek($resource, 0);
$body = $resource;
}

Expand Down Expand Up @@ -118,13 +118,13 @@ public function __toString()
throw $e;
}

if (\is_array($errorHandler = set_error_handler('var_dump'))) {
if (\is_array($errorHandler = \set_error_handler('var_dump'))) {
$errorHandler = $errorHandler[0] ?? null;
}
restore_error_handler();
\restore_error_handler();

if ($e instanceof \Error || $errorHandler instanceof SymfonyErrorHandler || $errorHandler instanceof SymfonyLegacyErrorHandler) {
return trigger_error((string) $e, \E_USER_ERROR);
return \trigger_error((string) $e, \E_USER_ERROR);
}

return '';
Expand All @@ -135,7 +135,7 @@ public function close(): void
{
if (isset($this->stream)) {
if (\is_resource($this->stream)) {
fclose($this->stream);
\fclose($this->stream);
}
$this->detach();
}
Expand Down Expand Up @@ -176,10 +176,10 @@ public function getSize(): ?int

// Clear the stat cache if the stream has a URI
if ($uri = $this->getUri()) {
clearstatcache(true, $uri);
\clearstatcache(true, $uri);
}

$stats = fstat($this->stream);
$stats = \fstat($this->stream);
if (isset($stats['size'])) {
$this->size = $stats['size'];

Expand All @@ -195,16 +195,16 @@ public function tell(): int
throw new \RuntimeException('Stream is detached');
}

if (false === $result = @ftell($this->stream)) {
throw new \RuntimeException('Unable to determine stream position: ' . (error_get_last()['message'] ?? ''));
if (false === $result = @\ftell($this->stream)) {
throw new \RuntimeException('Unable to determine stream position: ' . (\error_get_last()['message'] ?? ''));
}

return $result;
}

public function eof(): bool
{
return !isset($this->stream) || feof($this->stream);
return !isset($this->stream) || \feof($this->stream);
}

public function isSeekable(): bool
Expand All @@ -222,8 +222,8 @@ public function seek($offset, $whence = \SEEK_SET): void
throw new \RuntimeException('Stream is not seekable');
}

if (-1 === fseek($this->stream, $offset, $whence)) {
throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . var_export($whence, true));
if (-1 === \fseek($this->stream, $offset, $whence)) {
throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . \var_export($whence, true));
}
}

Expand All @@ -250,8 +250,8 @@ public function write($string): int
// We can't know the size after writing anything
$this->size = null;

if (false === $result = @fwrite($this->stream, $string)) {
throw new \RuntimeException('Unable to write to stream: ' . (error_get_last()['message'] ?? ''));
if (false === $result = @\fwrite($this->stream, $string)) {
throw new \RuntimeException('Unable to write to stream: ' . (\error_get_last()['message'] ?? ''));
}

return $result;
Expand All @@ -272,8 +272,8 @@ public function read($length): string
throw new \RuntimeException('Cannot read from non-readable stream');
}

if (false === $result = @fread($this->stream, $length)) {
throw new \RuntimeException('Unable to read from stream: ' . (error_get_last()['message'] ?? ''));
if (false === $result = @\fread($this->stream, $length)) {
throw new \RuntimeException('Unable to read from stream: ' . (\error_get_last()['message'] ?? ''));
}

return $result;
Expand All @@ -285,8 +285,8 @@ public function getContents(): string
throw new \RuntimeException('Stream is detached');
}

if (false === $contents = @stream_get_contents($this->stream)) {
throw new \RuntimeException('Unable to read stream contents: ' . (error_get_last()['message'] ?? ''));
if (false === $contents = @\stream_get_contents($this->stream)) {
throw new \RuntimeException('Unable to read stream contents: ' . (\error_get_last()['message'] ?? ''));
}

return $contents;
Expand All @@ -305,7 +305,7 @@ public function getMetadata($key = null)
return $key ? null : [];
}

$meta = stream_get_meta_data($this->stream);
$meta = \stream_get_meta_data($this->stream);

if (null === $key) {
return $meta;
Expand Down
12 changes: 6 additions & 6 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public function getStream(): StreamInterface
return $this->stream;
}

if (false === $resource = @fopen($this->file, 'r')) {
throw new \RuntimeException(sprintf('The file "%s" cannot be opened: %s', $this->file, error_get_last()['message'] ?? ''));
if (false === $resource = @\fopen($this->file, 'r')) {
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? ''));
}

return Stream::create($resource);
Expand All @@ -130,19 +130,19 @@ public function moveTo($targetPath): void
}

if (null !== $this->file) {
$this->moved = 'cli' === \PHP_SAPI ? @rename($this->file, $targetPath) : @move_uploaded_file($this->file, $targetPath);
$this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath);

if (false === $this->moved) {
throw new \RuntimeException(sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, error_get_last()['message'] ?? ''));
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? ''));
}
} else {
$stream = $this->getStream();
if ($stream->isSeekable()) {
$stream->rewind();
}

if (false === $resource = @fopen($targetPath, 'w')) {
throw new \RuntimeException(sprintf('The file "%s" cannot be opened: %s', $targetPath, error_get_last()['message'] ?? ''));
if (false === $resource = @\fopen($targetPath, 'w')) {
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? ''));
}

$dest = Stream::create($resource);
Expand Down
Loading

0 comments on commit 52887ca

Please sign in to comment.