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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Fixture;

use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

final class User extends Model
{
protected function getSomeAttribute(): mixed
{
}

protected function setSomeAttribute(): mixed
{
}

protected function foo(): Attribute
{
}

protected function scopeFoo(): mixed
{
}

#[Scope]
protected function bar(): mixed
{
}
}
79 changes: 79 additions & 0 deletions rules/Privatization/Guard/LaravelModelGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Rector\Privatization\Guard;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Util\StringUtils;

/**
* Guards against privatizing Laravel model attributes and scopes
*/
final readonly class LaravelModelGuard
{
/**
* @var string
* @see https://regex101.com/r/Dx0WN5/2
*/
private const LARAVEL_MODEL_ATTRIBUTE_REGEX = '#^[gs]et.+Attribute$#';

/**
* @var string
* @see https://regex101.com/r/hxOGeN/2
*/
private const LARAVEL_MODEL_SCOPE_REGEX = '#^scope.+$#';

public function __construct(
private PhpAttributeAnalyzer $phpAttributeAnalyzer,
private NodeNameResolver $nodeNameResolver,
private NodeTypeResolver $nodeTypeResolver,
) {
}

public function isProtectedMethod(ClassReflection $classReflection, ClassMethod $classMethod): bool
{
if (! $classReflection->is('Illuminate\Database\Eloquent\Model')) {
return false;
}

$name = (string) $this->nodeNameResolver->getName($classMethod->name);

return $this->isAttributeMethod($name, $classMethod)
|| $this->isScopeMethod($name, $classMethod);
}

private function isAttributeMethod(string $name, ClassMethod $classMethod): bool
{
if (StringUtils::isMatch($name, self::LARAVEL_MODEL_ATTRIBUTE_REGEX)) {
return true;
}

if (! $classMethod->returnType instanceof Node) {
return false;
}

return $this->nodeTypeResolver->isObjectType(
$classMethod->returnType,
new ObjectType('Illuminate\Database\Eloquent\Casts\Attribute')
);
}

private function isScopeMethod(string $name, ClassMethod $classMethod): bool
{
if (StringUtils::isMatch($name, self::LARAVEL_MODEL_SCOPE_REGEX)) {
return true;
}

return $this->phpAttributeAnalyzer->hasPhpAttribute(
$classMethod,
'Illuminate\Database\Eloquent\Attributes\Scope'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Reflection\ClassReflection;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\Privatization\Guard\LaravelModelGuard;
use Rector\Privatization\Guard\OverrideByParentClassGuard;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard;
Expand All @@ -28,6 +29,7 @@ public function __construct(
private readonly VisibilityManipulator $visibilityManipulator,
private readonly OverrideByParentClassGuard $overrideByParentClassGuard,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly LaravelModelGuard $laravelModelGuard,
) {
}

Expand Down Expand Up @@ -93,6 +95,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->laravelModelGuard->isProtectedMethod($classReflection, $classMethod)) {
continue;
}

if ($this->classMethodVisibilityGuard->isClassMethodVisibilityGuardedByParent(
$classMethod,
$classReflection
Expand Down
14 changes: 14 additions & 0 deletions stubs/Illuminate/Database/Eloquent/Attributes/Scope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes;

use Attribute;

if (class_exists('Illuminate\Database\Eloquent\Attributes\Scope')) {
return;
}

#[Attribute(Attribute::TARGET_METHOD)]
class Scope
{
}
11 changes: 11 additions & 0 deletions stubs/Illuminate/Database/Eloquent/Casts/Attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Database\Eloquent\Casts;

if (class_exists('Illuminate\Database\Eloquent\Casts\Attribute')) {
return;
}

class Attribute
{
}
11 changes: 11 additions & 0 deletions stubs/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Database\Eloquent;

if (class_exists('Illuminate\Database\Eloquent\Model')) {
return;
}

abstract class Model
{
}
Loading