Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RectorPHP #163

Draft
wants to merge 2 commits into
base: 3.1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -10,6 +10,7 @@
/phpunit.xml.dist export-ignore
/psalm-baseline.xml export-ignore
/psalm.xml.dist export-ignore
/rector.php export-ignore
/renovate.json export-ignore
/test/ export-ignore
/psalm/ export-ignore
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -48,6 +48,7 @@
"php-http/psr7-integration-tests": "^1.3",
"phpunit/phpunit": "^9.5.28",
"psalm/plugin-phpunit": "^0.18.4",
"rector/rector": "^0.17.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something I'd really like to see in our CI, rather than in a dev-dependency.

That said, it is fine here too, for now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no phar available for rector, and we could include it as a global dependency in CI, but that could lead to an introduced failure without any code changes in the repositories.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@internalsystemerror I would make a separate composer.lock just for that, installed globally in our CI container.

Not a problem for now though: just raising the idea :-)

Related: laminas/laminas-ci-matrix-action#131

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No please keep it coming, thats the point of this PR as a draft, to get feedback!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinned version 0.17.0 is better imo

"vimeo/psalm": "^5.9"
},
"provide": {
Expand Down
125 changes: 124 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions phpcs.xml
Expand Up @@ -13,6 +13,7 @@
<arg value="ps"/>

<!-- Paths to check -->
<file>rector.php</file>
<file>src</file>
<file>test</file>

Expand Down
10 changes: 1 addition & 9 deletions psalm-baseline.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.10.0@a5effd2d2dddd1a7ea7a0f6a051ce63ff979e356">
<files psalm-version="5.12.0@f90118cdeacd0088e7215e64c0c99ceca819e176">
<file src="src/CallbackStream.php">
<ImplementedReturnTypeMismatch>
<code>null|callable</code>
Expand Down Expand Up @@ -146,14 +146,6 @@
<code>json_encode</code>
</UnusedFunctionCall>
</file>
<file src="src/Response/RedirectResponse.php">
<DocblockTypeContradiction>
<code>gettype($uri)</code>
</DocblockTypeContradiction>
<RedundantConditionGivenDocblockType>
<code>is_object($uri)</code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/Response/Serializer.php">
<MixedArgument>
<code>$body</code>
Expand Down
1 change: 1 addition & 0 deletions psalm.xml.dist
Expand Up @@ -10,6 +10,7 @@
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<file name="rector.php"/>
<directory name="src"/>
<directory name="test"/>
<ignoreFiles>
Expand Down
19 changes: 19 additions & 0 deletions rector.php
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
// Set paths
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/test',
]);

// Define set list to upgrade PHP
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my current config for some laminas components I run:

<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
use Rector\Config\RectorConfig;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
use Rector\Php70\Rector\FuncCall\RandomFunctionRector;
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->sets([
        LevelSetList::UP_TO_PHP_80,
    ]);

    $rectorConfig->rules([
        StaticArrowFunctionRector::class,
        StaticClosureRector::class,
        AddClosureReturnTypeRector::class,
        PrivatizeFinalClassPropertyRector::class,
        AddArrowFunctionReturnTypeRector::class,
    ]);

    $rectorConfig->parallel();
    $rectorConfig->paths([
        __DIR__ . '/src',
        __DIR__ . '/test',
    ]);

    $rectorConfig->skip([
        // possibly too detail on some cases?
        CountOnNullRector::class,

        // possibly null undefined on purpose?
        AddDefaultValueForUndefinedVariableRector::class,

        // define list of services?
        StringClassNameToClassConstantRector::class,

        // not a secure lib on purpose?
        RandomFunctionRector::class,

        // probably handle after execute?
        JsonThrowOnErrorRector::class =>  [
            __DIR__ . '/src',
        ],
    ]);
};

5 changes: 2 additions & 3 deletions src/Exception/InvalidForwardedHeaderNameException.php
Expand Up @@ -6,8 +6,7 @@

use Laminas\Diactoros\ServerRequestFilter\FilterUsingXForwardedHeaders;

use function gettype;
use function is_object;
use function get_debug_type;
use function is_string;
use function sprintf;

Expand All @@ -16,7 +15,7 @@ class InvalidForwardedHeaderNameException extends RuntimeException implements Ex
public static function forHeader(mixed $name): self
{
if (! is_string($name)) {
$name = sprintf('(value of type %s)', is_object($name) ? $name::class : gettype($name));
$name = sprintf('(value of type %s)', get_debug_type($name));
}

return new self(sprintf(
Expand Down
5 changes: 2 additions & 3 deletions src/Exception/InvalidProxyAddressException.php
Expand Up @@ -4,15 +4,14 @@

namespace Laminas\Diactoros\Exception;

use function gettype;
use function is_object;
use function get_debug_type;
use function sprintf;

class InvalidProxyAddressException extends RuntimeException implements ExceptionInterface
{
public static function forInvalidProxyArgument(mixed $proxy): self
{
$type = is_object($proxy) ? $proxy::class : gettype($proxy);
$type = get_debug_type($proxy);
return new self(sprintf(
'Invalid proxy of type "%s" provided;'
. ' must be a valid IPv4 or IPv6 address, optionally with a subnet mask provided'
Expand Down
7 changes: 3 additions & 4 deletions src/HeaderSecurity.php
Expand Up @@ -4,10 +4,9 @@

namespace Laminas\Diactoros;

use function gettype;
use function get_debug_type;
use function in_array;
use function is_numeric;
use function is_object;
use function is_string;
use function ord;
use function preg_match;
Expand Down Expand Up @@ -128,7 +127,7 @@ public static function assertValid(mixed $value): void
if (! is_string($value) && ! is_numeric($value)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid header value type; must be a string or numeric; received %s',
is_object($value) ? $value::class : gettype($value)
get_debug_type($value)
));
}
if (! self::isValid($value)) {
Expand All @@ -151,7 +150,7 @@ public static function assertValidName(mixed $name): void
if (! is_string($name)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid header name type; expected string; received %s',
is_object($name) ? $name::class : gettype($name)
get_debug_type($name)
));
}
if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/D', $name)) {
Expand Down
5 changes: 2 additions & 3 deletions src/Response/HtmlResponse.php
Expand Up @@ -9,8 +9,7 @@
use Laminas\Diactoros\Stream;
use Psr\Http\Message\StreamInterface;

use function gettype;
use function is_object;
use function get_debug_type;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -60,7 +59,7 @@ private function createBody($html): StreamInterface
if (! is_string($html)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid content (%s) provided to %s',
is_object($html) ? $html::class : gettype($html),
get_debug_type($html),
self::class
));
}
Expand Down
5 changes: 2 additions & 3 deletions src/Response/RedirectResponse.php
Expand Up @@ -8,8 +8,7 @@
use Laminas\Diactoros\Response;
use Psr\Http\Message\UriInterface;

use function gettype;
use function is_object;
use function get_debug_type;
use function is_string;
use function sprintf;

Expand All @@ -36,7 +35,7 @@ public function __construct($uri, int $status = 302, array $headers = [])
throw new Exception\InvalidArgumentException(sprintf(
'Uri provided to %s MUST be a string or Psr\Http\Message\UriInterface instance; received "%s"',
self::class,
is_object($uri) ? $uri::class : gettype($uri)
get_debug_type($uri)
));
}

Expand Down
5 changes: 2 additions & 3 deletions src/Response/TextResponse.php
Expand Up @@ -9,8 +9,7 @@
use Laminas\Diactoros\Stream;
use Psr\Http\Message\StreamInterface;

use function gettype;
use function is_object;
use function get_debug_type;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -60,7 +59,7 @@ private function createBody($text): StreamInterface
if (! is_string($text)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid content (%s) provided to %s',
is_object($text) ? $text::class : gettype($text),
get_debug_type($text),
self::class
));
}
Expand Down
5 changes: 2 additions & 3 deletions src/Response/XmlResponse.php
Expand Up @@ -9,8 +9,7 @@
use Laminas\Diactoros\Stream;
use Psr\Http\Message\StreamInterface;

use function gettype;
use function is_object;
use function get_debug_type;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -62,7 +61,7 @@ private function createBody($xml): StreamInterface
if (! is_string($xml)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid content (%s) provided to %s',
is_object($xml) ? $xml::class : gettype($xml),
get_debug_type($xml),
self::class
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/ServerRequestFactory.php
Expand Up @@ -54,7 +54,7 @@ public static function fromGlobals(
?array $files = null,
?FilterServerRequestInterface $requestFilter = null
): ServerRequestInterface {
$requestFilter = $requestFilter ?? FilterUsingXForwardedHeaders::trustReservedSubnets();
$requestFilter ??= FilterUsingXForwardedHeaders::trustReservedSubnets();

$server = normalizeServer(
$server ?? $_SERVER,
Expand Down
3 changes: 1 addition & 2 deletions src/UploadedFile.php
Expand Up @@ -50,8 +50,7 @@ class UploadedFile implements UploadedFileInterface

private bool $moved = false;

/** @var null|StreamInterface */
private $stream;
private ?StreamInterface $stream = null;

/**
* @param string|resource|StreamInterface $streamOrFile
Expand Down