Skip to content

Commit fb58990

Browse files
committed
extract LaravelClassName
1 parent 11dbba6 commit fb58990

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector/Fixture/skip_protected_magic_called_from_parent.php.inc

Lines changed: 0 additions & 15 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Fixture;
4+
5+
use PhpParser\PrettyPrinter\Standard;
6+
use Rector\PhpParser\Node\FileNode;
7+
8+
final class KeepMagicParentCalledMethod extends Standard
9+
{
10+
// called from parent by $this->{'p'} method
11+
protected function pFileNode(FileNode $fileNode)
12+
{
13+
}
14+
}

rules/Privatization/Guard/LaravelModelGuard.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PHPStan\Reflection\ClassReflection;
1010
use PHPStan\Type\ObjectType;
11+
use Rector\Enum\LaravelClassName;
1112
use Rector\NodeNameResolver\NodeNameResolver;
1213
use Rector\NodeTypeResolver\NodeTypeResolver;
1314
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
@@ -39,7 +40,7 @@ public function __construct(
3940

4041
public function isProtectedMethod(ClassReflection $classReflection, ClassMethod $classMethod): bool
4142
{
42-
if (! $classReflection->is('Illuminate\Database\Eloquent\Model')) {
43+
if (! $classReflection->is(LaravelClassName::MODEL)) {
4344
return false;
4445
}
4546

@@ -63,7 +64,7 @@ private function isAttributeMethod(string $name, ClassMethod $classMethod): bool
6364

6465
return $this->nodeTypeResolver->isObjectType(
6566
$classMethod->returnType,
66-
new ObjectType('Illuminate\Database\Eloquent\Casts\Attribute')
67+
new ObjectType(LaravelClassName::CAST_ATTRIBUTE)
6768
);
6869
}
6970

@@ -73,9 +74,6 @@ private function isScopeMethod(string $name, ClassMethod $classMethod): bool
7374
return true;
7475
}
7576

76-
return $this->phpAttributeAnalyzer->hasPhpAttribute(
77-
$classMethod,
78-
'Illuminate\Database\Eloquent\Attributes\Scope'
79-
);
77+
return $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, LaravelClassName::ATTRIBUTES_SCOPE);
8078
}
8179
}

rules/Privatization/Rector/ClassMethod/PrivatizeFinalClassMethodRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function refactor(Node $node): ?Node
8383
}
8484

8585
$scope = ScopeFetcher::fetch($node);
86+
8687
$classReflection = $scope->getClassReflection();
8788
if (! $classReflection instanceof ClassReflection) {
8889
return null;

src/Enum/LaravelClassName.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Enum;
6+
7+
final class LaravelClassName
8+
{
9+
public const MODEL = 'Illuminate\Database\Eloquent\Model';
10+
11+
public const CAST_ATTRIBUTE = 'Illuminate\Database\Eloquent\Casts\Attribute';
12+
13+
public const ATTRIBUTES_SCOPE = 'Illuminate\Database\Eloquent\Attributes\Scope';
14+
}

0 commit comments

Comments
 (0)