Skip to content
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
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<directory>tests</directory>
<directory>rules-tests</directory>
<directory>utils-tests</directory>
<exclude>rules-tests/Php83/Rector/ClassMethod/AddOverrideAttributeToOverriddenMethodsRector/Source/SomeAbstractTest.php</exclude>
</testsuite>
</testsuites>

Expand Down
8 changes: 2 additions & 6 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector;
use Rector\Utils\Rector\RemoveRefactorDuplicatedNodeInstanceCheckRector;

Expand All @@ -26,7 +25,7 @@
)
->withAttributesSets()
->withComposerBased(phpunit: true)
->withPhpSets(php82: true)
->withPhpSets()
->withPaths([
__DIR__ . '/bin',
__DIR__ . '/config',
Expand All @@ -40,10 +39,7 @@
])
->withRootFiles()
->withImportNames(removeUnusedImports: true)
->withRules([
RemoveRefactorDuplicatedNodeInstanceCheckRector::class, AddSeeTestAnnotationRector::class,
AddTypeToConstRector::class,
])
->withRules([RemoveRefactorDuplicatedNodeInstanceCheckRector::class, AddSeeTestAnnotationRector::class])
->withSkip([
StringClassNameToClassConstantRector::class,
// tests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Fixture;

use Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source\SomeAbstractTest;

final class SkipSetupPHPUnitAsClutter extends SomeAbstractTest
{
protected function setUp(): void
{
parent::setUp();

$someValue = 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector\Source;

use PHPUnit\Framework\TestCase;

abstract class SomeAbstractTest extends TestCase
{
protected function setUp(): void
{
$value = 1000;
}
}
53 changes: 53 additions & 0 deletions rules/DeadCode/NodeAnalyzer/ParentClassAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\NodeAnalyzer;

use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeNameResolver\NodeNameResolver;

final readonly class ParentClassAnalyzer
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
) {

}

public function hasParentCall(ClassMethod $classMethod): bool
{
if ($classMethod->isAbstract()) {
return false;
}

if ($classMethod->isPrivate()) {
return false;
}

$classMethodName = $classMethod->name->name;

foreach ((array) $classMethod->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

$expr = $stmt->expr;
if (! $expr instanceof StaticCall) {
continue;
}

if (! $this->nodeNameResolver->isName($expr->class, 'parent')) {
continue;
}

if ($this->nodeNameResolver->isName($expr->name, $classMethodName)) {
return true;
}
}

return false;
}
}
2 changes: 2 additions & 0 deletions rules/Php81/Rector/Array_/FirstClassCallableRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Php81\Rector\Array_;

use Override;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
Expand All @@ -15,6 +16,7 @@
*/
final class FirstClassCallableRector extends ArrayToFirstClassCallableRector implements DeprecatedInterface
{
#[Override]
public function refactor(Node $node): StaticCall|MethodCall|null
{
throw new ShouldNotHappenException(sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\DeadCode\NodeAnalyzer\ParentClassAnalyzer;
use Rector\NodeAnalyzer\ClassAnalyzer;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\PhpParser\AstResolver;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function __construct(
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly AstResolver $astResolver,
private readonly ValueResolver $valueResolver,
private readonly ParentClassAnalyzer $parentClassAnalyzer,
) {
}

Expand Down Expand Up @@ -135,6 +137,11 @@ public function refactor(Node $node): ?Node
return null;
}

// skip if no parents, nor traits, nor strinables are involved
if ($node->extends === null && $node->getTraitUses() === [] && ! $this->implementsStringable($node)) {
return null;
}

$className = (string) $this->getName($node);
if (! $this->reflectionProvider->hasClass($className)) {
return null;
Expand Down Expand Up @@ -227,12 +234,18 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

// nothing to override
if ($classMethod->isPrivate()) {
return true;
}

// ignore if it already uses the attribute
return $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::OVERRIDE_CLASS);
if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::OVERRIDE_CLASS)) {
return true;
}

// skip test setup method override, as rather clutters the code than helps
return $this->isName($classMethod, 'setUp') && $this->parentClassAnalyzer->hasParentCall($classMethod);
}

private function shouldSkipParentClassMethod(ClassReflection $parentClassReflection, ClassMethod $classMethod): bool
Expand Down Expand Up @@ -284,4 +297,15 @@ private function shouldSkipParentClassMethod(ClassReflection $parentClassReflect

return false;
}

private function implementsStringable(Class_ $class): bool
{
foreach ($class->implements as $implement) {
if ($this->isName($implement, 'Stringable')) {
return true;
}
}

return false;
}
}
2 changes: 2 additions & 0 deletions src/BetterPhpDocParser/PhpDoc/SpacelessPhpDocTagNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDoc;

use Override;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Stringable;

Expand All @@ -13,6 +14,7 @@
*/
final class SpacelessPhpDocTagNode extends PhpDocTagNode implements Stringable
{
#[Override]
public function __toString(): string
{
return $this->name . $this->value;
Expand Down
3 changes: 3 additions & 0 deletions src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\BetterPhpDocParser\PhpDocParser;

use Nette\Utils\Strings;
use Override;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
Expand Down Expand Up @@ -90,6 +91,7 @@ public function parseWithNode(BetterTokenIterator $betterTokenIterator, Node $no
return $phpDocNode;
}

#[Override]
public function parseTag(TokenIterator $tokenIterator): PhpDocTagNode
{
// replace generic nodes with DoctrineAnnotations
Expand All @@ -106,6 +108,7 @@ public function parseTag(TokenIterator $tokenIterator): PhpDocTagNode
/**
* @param BetterTokenIterator $tokenIterator
*/
#[Override]
public function parseTagValue(TokenIterator $tokenIterator, string $tag): PhpDocTagValueNode
{
$isPrecededByHorizontalWhitespace = $tokenIterator->isPrecededByHorizontalWhitespace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\ValueObject\PhpDoc;

use Override;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use Stringable;
Expand All @@ -19,6 +20,7 @@ public function __construct(
parent::__construct($name, $typeNode, $description);
}

#[Override]
public function __toString(): string
{
// @see https://github.com/rectorphp/rector/issues/3438
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Rector\BetterPhpDocParser\ValueObject\Type;

use Override;
use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode;
use Stringable;

final class BracketsAwareIntersectionTypeNode extends IntersectionTypeNode implements Stringable
{
#[Override]
public function __toString(): string
{
return implode('&', $this->types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\ValueObject\Type;

use Override;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Stringable;
Expand All @@ -23,6 +24,7 @@ public function __construct(
/**
* Preserve common format
*/
#[Override]
public function __toString(): string
{
$types = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Rector\BetterPhpDocParser\ValueObject\Type;

use Override;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Stringable;

final class FullyQualifiedIdentifierTypeNode extends IdentifierTypeNode implements Stringable
{
#[Override]
public function __toString(): string
{
return '\\' . ltrim($this->name, '\\');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\ValueObject\Type;

use Override;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
Expand All @@ -14,6 +15,7 @@

final class SpacingAwareArrayTypeNode extends ArrayTypeNode implements Stringable
{
#[Override]
public function __toString(): string
{
if ($this->type instanceof CallableTypeNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Rector\BetterPhpDocParser\ValueObject\Type;

use Override;
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
use Stringable;

final class SpacingAwareCallableTypeNode extends CallableTypeNode implements Stringable
{
#[Override]
public function __toString(): string
{
// keep original (Psalm?) format, see https://github.com/rectorphp/rector/issues/2841
Expand Down
2 changes: 2 additions & 0 deletions src/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Config;

use Illuminate\Container\Container;
use Override;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
Expand Down Expand Up @@ -406,6 +407,7 @@ public function autotagInterface(string $interface): void
/**
* @param string $abstract
*/
#[Override]
public function singleton($abstract, mixed $concrete = null): void
{
parent::singleton($abstract, $concrete);
Expand Down
3 changes: 3 additions & 0 deletions src/Console/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Console;

use Composer\XdebugHandler\XdebugHandler;
use Override;
use Rector\Application\VersionResolver;
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Configuration\Option;
Expand Down Expand Up @@ -37,6 +38,7 @@ public function __construct(array $commands)
$this->setDefaultCommand('process');
}

#[Override]
public function doRun(InputInterface $input, OutputInterface $output): int
{
$this->enableXdebug($input);
Expand Down Expand Up @@ -76,6 +78,7 @@ public function doRun(InputInterface $input, OutputInterface $output): int
return parent::doRun($input, $output);
}

#[Override]
protected function getDefaultInputDefinition(): InputDefinition
{
$defaultInputDefinition = parent::getDefaultInputDefinition();
Expand Down
3 changes: 3 additions & 0 deletions src/Console/Style/RectorStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Console\Style;

use OndraM\CiDetector\CiDetector;
use Override;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -30,6 +31,7 @@ public function __construct(InputInterface $input, OutputInterface $output)
/**
* @see https://github.com/phpstan/phpstan-src/commit/0993d180e5a15a17631d525909356081be59ffeb
*/
#[Override]
public function createProgressBar(int $max = 0): ProgressBar
{
$progressBar = parent::createProgressBar($max);
Expand All @@ -56,6 +58,7 @@ public function createProgressBar(int $max = 0): ProgressBar
return $progressBar;
}

#[Override]
public function progressAdvance(int $step = 1): void
{
// hide progress bar in tests
Expand Down
Loading