Skip to content

Commit

Permalink
[CI] Just some code style changes (#232)
Browse files Browse the repository at this point in the history
* Update php-cs-fixer

* CS fixes

* CS native_function_invocation

* CS native_constant_invocation

* CS ordered_imports

* CS declare_strict_types

* CS linebreak_after_opening_tag

* fix

* update meta files
  • Loading branch information
Nyholm committed Apr 11, 2023
1 parent be5f75a commit 1cb9982
Show file tree
Hide file tree
Showing 27 changed files with 190 additions and 168 deletions.
60 changes: 38 additions & 22 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,63 @@ name: Static analysis
jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: none
tools: phpstan:1.8.11, cs2pr

- name: Download dependencies
run: |
composer update --no-interaction --prefer-dist --optimize-autoloader
uses: ramsey/composer-install@v2

- name: PHPStan
uses: docker://oskarstark/phpstan-ga:0.12.48
env:
REQUIRE_DEV: true
with:
entrypoint: /composer/vendor/bin/phpstan
args: analyze --no-progress
run: phpstan analyze --no-progress --error-format=checkstyle | cs2pr

php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: PHP-CS-Fixer
uses: OskarStark/php-cs-fixer-ga@2.17.3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
args: --dry-run --diff-format udiff
php-version: 8.1
coverage: none
tools: php-cs-fixer:3.15, cs2pr

- name: Display PHP-CS-Fixer version
run: php-cs-fixer --version

- name: PHP-CS-Fixer
run: php-cs-fixer fix --dry-run --format=checkstyle | cs2pr

psalm:
name: Psalm
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Psalm
uses: docker://vimeo/psalm-github-actions:3.17.2
env:
REQUIRE_DEV: true
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
args: --no-progress --show-info=false --stats
php-version: 8.1
extensions: apcu, redis
coverage: none
tools: vimeo/psalm:4.29.0

- name: Download dependencies
uses: ramsey/composer-install@v2

- name: Psalm
run: psalm --no-progress --output-format=github
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ composer.lock
phpstan.neon
phpunit.xml
vendor
.php_cs.cache
.php-cs-fixer.cache
.phpunit.result.cache
.tmp
26 changes: 26 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests');

$config = new PhpCsFixer\Config();

return $config->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => array('syntax' => 'short'),
'native_function_invocation' => true,
'native_constant_invocation' => true,
'ordered_imports' => true,
'declare_strict_types' => false,
'linebreak_after_opening_tag' => false,
'single_import_per_statement' => false,
'blank_line_after_opening_tag' => false,
'concat_space' => ['spacing'=>'one'],
'phpdoc_align' => ['align'=>'left'],
])
->setRiskyAllowed(true)
->setFinder($finder);
24 changes: 0 additions & 24 deletions .php_cs

This file was deleted.

14 changes: 4 additions & 10 deletions phpstan.baseline.dist → phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,21 @@ parameters:
path: src/ServerRequest.php

-
message: "#^Parameter \\#1 \\$error_handler of function set_error_handler expects \\(callable\\(int, string, string, int, array\\)\\: bool\\)\\|null, 'var_dump' given\\.$#"
message: "#^Parameter \\#1 \\$callback of function set_error_handler expects \\(callable\\(int, string, string, int\\)\\: bool\\)\\|null, 'var_dump' given\\.$#"
count: 1
path: src/Stream.php

-
message: "#^Method Nyholm\\\\Psr7\\\\Stream\\:\\:__toString\\(\\) should return string but returns bool\\.$#"
message: "#^Result of && is always false\\.$#"
count: 1
path: src/Stream.php

-
message: "#^Strict comparison using \\=\\=\\= between false and true will always evaluate to false\\.$#"
message: "#^Result of && is always false\\.$#"
count: 2
path: src/UploadedFile.php

-
message: "#^Result of && is always false\\.$#"
message: "#^Strict comparison using \\=\\=\\= between false and true will always evaluate to false\\.$#"
count: 2
path: src/UploadedFile.php

-
message: "#^Result of && is always false\\.$#"
count: 1
path: src/Stream.php

2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
includes:
- phpstan.baseline.dist
- phpstan-baseline.neon

parameters:
level: 5
Expand Down
8 changes: 8 additions & 0 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
</NoValue>
</file>
</files>
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"

errorBaseline="psalm.baseline.xml"
>
<projectFiles>
<directory name="src" />
Expand Down
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
Loading

0 comments on commit 1cb9982

Please sign in to comment.