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

Update Symplify deprecations #3298

Merged
merged 3 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@
"symfony/dependency-injection": "^4.4.6|^5.0.6",
"symfony/finder": "^4.4.6|^5.0.6",
"symfony/process": "^4.4.6|^5.0.6",
"symplify/auto-bind-parameter": "^7.2.4",
"symplify/autowire-array-parameter": "^7.2.4",
"symplify/package-builder": "^7.2.4",
"symplify/set-config-resolver": "^7.2.4",
"symplify/auto-bind-parameter": "^7.3.5",
"symplify/autowire-array-parameter": "^7.3.5",
"symplify/package-builder": "^7.3.5",
"symplify/set-config-resolver": "^7.3.5",
"tracy/tracy": "^2.7"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"ocramius/package-versions": "^1.4|^1.5",
"phpunit/phpunit": "^8.4|^9.0",
"slam/phpstan-extensions": "^5.0",
"symplify/changelog-linker": "^7.2.4",
"symplify/easy-coding-standard": "^7.2.4",
"symplify/monorepo-builder": "^7.2.4",
"symplify/phpstan-extensions": "^7.2.4",
"symplify/changelog-linker": "^7.3.5",
"symplify/easy-coding-standard": "^7.3.5",
"symplify/monorepo-builder": "^7.3.5",
"symplify/phpstan-extensions": "^7.3.5",
"thecodingmachine/phpstan-strict-rules": "^0.12"
},
"replace": {
Expand Down
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
exclude:
- '../src/Rector/**/*Rector.php'
- '../src/Testing/PHPUnit/*'
- '../src/Testing/Dumper/*'
- '../src/RectorDefinition/*'
- '../src/Exception/*'
- '../src/DependencyInjection/CompilerPass/*'
Expand Down
42 changes: 0 additions & 42 deletions ecs.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
services:
Symplify\CodingStandard\Sniffs\CleanCode\ClassCognitiveComplexitySniff:
max_class_cognitive_complexity: 50

Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff:
max_cognitive_complexity: 9

Symplify\CodingStandard\Fixer\Order\MethodOrderByTypeFixer:
method_order_by_type:
Rector\Contract\Rector\PhpRectorInterface:
- 'getDefinition'
- 'getNodeTypes'
- 'refactor'

Symplify\CodingStandard\Fixer\Naming\PropertyNameMatchingTypeFixer:
extra_skipped_classes:
- 'PhpParser\PrettyPrinter\Standard'
- '?string' # bug probably

Symplify\CodingStandard\Sniffs\Naming\ClassNameSuffixByParentSniff:
extra_parent_types_to_suffixes:
- 'Rector'

parameters:
paths:
- "bin"
Expand Down Expand Up @@ -88,33 +73,6 @@ parameters:
- 'src/Rector/AbstractRector.php'
- 'packages/post-rector/src/Rector/AbstractPostRector.php'

Symplify\CodingStandard\Sniffs\CleanCode\ClassCognitiveComplexitySniff:
# complex case
- 'packages/better-php-doc-parser/src/PhpDocInfo/PhpDocInfo.php'
# node printing
- 'utils/documentation-generator/src/Command/DumpNodesCommand.php'
# 3rd party code
- 'rules/php70/src/EregToPcreTransformer.php'

Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff:
# todo
- "packages/better-php-doc-parser/src/Printer/WhitespaceDetector.php"
- "packages/node-type-resolver/src/NodeTypeResolver.php"
- "packages/better-php-doc-parser/src/Printer/OriginalSpacingRestorer.php"
# @todo split to multiple rectors
- "rules/php-spec-to-phpunit/src/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php"

- 'packages/better-php-doc-parser/src/PhpDocNode/**/*TagValueNode.php'
- "rules/coding-style/src/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php"

# per node logic
- 'utils/documentation-generator/src/Command/DumpNodesCommand.php'
# copied 3rd party logic
- 'rules/php70/src/EregToPcreTransformer.php'
# dev
- 'packages/type-declaration/src/Rector/FunctionLike/*TypeDeclarationRector.php'
- 'rules/php70/src/Rector/If_/IfToSpaceshipRector.php'

PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\AssignmentInConditionSniff.FoundInWhileCondition: null

SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingParameterTypeHint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Rector\ConsoleDiffer\DifferAndFormatter;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\Error\ExceptionCorrector;
use Rector\Core\ValueObject\Application\Error;
use Rector\Core\ValueObject\Application\RectorError;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\PostRector\Collector\NodesToRemoveCollector;
use Symplify\SmartFileSystem\SmartFileInfo;
Expand All @@ -20,7 +20,7 @@
final class ErrorAndDiffCollector
{
/**
* @var Error[]
* @var RectorError[]
*/
private $errors = [];

Expand Down Expand Up @@ -69,7 +69,7 @@ public function __construct(
}

/**
* @return Error[]
* @return RectorError[]
*/
public function getErrors(): array
{
Expand Down Expand Up @@ -141,15 +141,15 @@ public function addAutoloadError(AnalysedCodeException $analysedCodeException, S
{
$message = $this->exceptionCorrector->getAutoloadExceptionMessageAndAddLocation($analysedCodeException);

$this->errors[] = new Error($fileInfo, $message);
$this->errors[] = new RectorError($fileInfo, $message);
}

public function addErrorWithRectorClassMessageAndFileInfo(
string $rectorClass,
string $message,
SmartFileInfo $smartFileInfo
): void {
$this->errors[] = new Error($smartFileInfo, $message, null, $rectorClass);
$this->errors[] = new RectorError($smartFileInfo, $message, null, $rectorClass);
}

public function addThrowableWithFileInfo(Throwable $throwable, SmartFileInfo $fileInfo): void
Expand All @@ -158,7 +158,7 @@ public function addThrowableWithFileInfo(Throwable $throwable, SmartFileInfo $fi
if ($rectorClass) {
$this->addErrorWithRectorClassMessageAndFileInfo($rectorClass, $throwable->getMessage(), $fileInfo);
} else {
$this->errors[] = new Error($fileInfo, $throwable->getMessage(), $throwable->getCode());
$this->errors[] = new RectorError($fileInfo, $throwable->getMessage(), $throwable->getCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Rector\Core\Configuration\Configuration;
use Rector\Core\Configuration\Option;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\ValueObject\Application\Error;
use Rector\Core\ValueObject\Application\RectorError;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down Expand Up @@ -122,7 +122,7 @@ private function reportFileDiffs(array $fileDiffs): void
}

/**
* @param Error[] $errors
* @param RectorError[] $errors
*/
private function reportErrors(array $errors): void
{
Expand Down
24 changes: 16 additions & 8 deletions packages/node-type-resolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,9 @@ private function isMatchingUnionType(Type $requiredType, Type $resolvedType): bo

private function resolveFirstType(Node $node): Type
{
foreach ($this->nodeTypeResolvers as $nodeTypeResolver) {
foreach ($nodeTypeResolver->getNodeClasses() as $nodeClass) {
if (! is_a($node, $nodeClass)) {
continue;
}

return $nodeTypeResolver->resolve($node);
}
$type = $this->resolveByNodeTypeResolvers($node);
if ($type !== null) {
return $type;
}

/** @var Scope|null $nodeScope */
Expand Down Expand Up @@ -346,4 +341,17 @@ private function isAnonymousClass(Node $node): bool

return $className === null || Strings::contains($className, 'AnonymousClass');
}

private function resolveByNodeTypeResolvers(Node $node): ?Type
{
foreach ($this->nodeTypeResolvers as $nodeClass => $nodeTypeResolver) {
if (! is_a($node, $nodeClass)) {
continue;
}

return $nodeTypeResolver->resolve($node);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function provideData(): Iterator
/**
* @param Node[] $nodes
*/
protected function visitNodes(array $nodes): void
protected function traverseNodes(array $nodes): void
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NameResolver());
Expand Down
52 changes: 48 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
- vendor/slam/phpstan-extensions/conf/slam-rules.neon

# see https://github.com/symplify/coding-standard
- vendor/symplify/coding-standard/config/symplify-rules.neon

parameters:
level: max

# see https://github.com/symplify/coding-standard
symplify:
max_cognitive_complexity: 9 # default: 8
parent_classes:
- Rector

# to allow installing with various phsptan versions without reporting old errors here
reportUnmatchedIgnoredErrors: false

Expand All @@ -27,19 +36,15 @@ parameters:
- 'packages/doctrine-annotation-generated/src/ConstantPreservingDocParser.php'
- 'packages/doctrine-annotation-generated/src/ConstantPreservingAnnotationReader.php'

- ci/check_services_in_yaml_configs.php
- "*/Expected/*"
# complex printer
- "utils/phpstan/generate-paths.php"
- '*tests/Rector/MethodCall/RenameMethodRector/**/SomeClass.php'
# tests files
- '*tests/*/Fixture/*'
- '*tests/*/Source/*'
- '*tests/Source/*'
# part of composer
- '*/tests/Rector/Psr4/MultipleClassFileToPsr4ClassesRector/Expected/Just*ExceptionWithoutNamespace.php'
# stubs
- 'stubs/*'

ignoreErrors:
# false positive
Expand Down Expand Up @@ -257,3 +262,42 @@ parameters:
message: "#^Class \"Rector\\\\PSR4\\\\Rector\\\\Namespace_\\\\NormalizeNamespaceByPSR4ComposerAutoloadRector\" is missing @see annotation with test case class reference$#"
count: 1
path: rules/psr4/src/Rector/Namespace_/NormalizeNamespaceByPSR4ComposerAutoloadRector.php

- '#Method Rector\\Core\\Testing\\Finder\\RectorsFinder\:\:findInDirectories\(\) should return array<Rector\\Core\\Contract\\Rector\\RectorInterface\> but returns array<object\>#'

-
message: "#^Class cognitive complexity for \"PhpDocInfo\" is 53, keep it under 50$#"
count: 1
path: packages/better-php-doc-parser/src/PhpDocInfo/PhpDocInfo.php

-
message: "#^Cognitive complexity for \"Rector\\\\BetterPhpDocParser\\\\Printer\\\\WhitespaceDetector\\:\\:detectOldWhitespaces\\(\\)\" is 18, keep it under 9$#"
count: 1
path: packages/better-php-doc-parser/src/Printer/WhitespaceDetector.php

-
message: "#^Parameter \\#1 \\$input of function array_splice expects array, array\\<PhpParser\\\\Node\\\\Stmt\\>\\|null given\\.$#"
count: 1
path: rules/coding-style/src/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php

-
message: "#^Cognitive complexity for \"Rector\\\\PhpSpecToPHPUnit\\\\Rector\\\\MethodCall\\\\PhpSpecPromisesToPHPUnitAssertRector\\:\\:refactor\\(\\)\" is 13, keep it under 9$#"
count: 1
path: rules/php-spec-to-phpunit/src/Rector/MethodCall/PhpSpecPromisesToPHPUnitAssertRector.php

-
message: "#^Class cognitive complexity for \"EregToPcreTransformer\" is 77, keep it under 50$#"
count: 1
path: rules/php70/src/EregToPcreTransformer.php

-
message: "#^Cognitive complexity for \"Rector\\\\Php70\\\\EregToPcreTransformer\\:\\:(.*?)\" is (.*?), keep it under 9$#"
path: rules/php70/src/EregToPcreTransformer.php

-
message: "#^Cognitive complexity for \"Rector\\\\Core\\\\Testing\\\\Dumper\\\\AttributeFilteringNodeDumper\\:\\:dumpSubNodes\\(\\)\" is 16, keep it under 9$#"
count: 1
path: src/Testing/Dumper/AttributeFilteringNodeDumper.php


- '#Use explicit return value over magic &reference#'
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,10 @@ public function refactor(Node $node): ?Node

$hasChanged = false;

if ($node->stmts === null) {
return null;
}

foreach ($node->stmts as $key => $stmt) {
foreach ((array) $node->stmts as $key => $stmt) {
$currentStmtVariableName = null;

if ($stmt instanceof Expression) {
$stmt = $stmt->expr;
}
$stmt = $this->unwrapExpression($stmt);

if ($stmt instanceof Assign || $stmt instanceof MethodCall) {
if ($this->shouldSkipLeftVariable($stmt)) {
Expand All @@ -111,11 +105,7 @@ public function refactor(Node $node): ?Node
$this->previousStmtVariableName = $currentStmtVariableName;
}

if (! $hasChanged) {
return null;
}

return $node;
return $hasChanged ? $node : null;
}

private function reset(): void
Expand Down Expand Up @@ -180,4 +170,13 @@ private function isPreceededByEmptyLine(Node $node, int $key): bool

return abs($currentNode->getLine() - $previousNode->getLine()) >= 2;
}

private function unwrapExpression(Node $node): Node
{
if ($node instanceof Expression) {
return $node->expr;
}

return $node;
}
}
Loading