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

🚀 Optimize Model attribute existence check #306

Merged
merged 2 commits into from
Jan 29, 2023
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
8 changes: 2 additions & 6 deletions src/Handlers/Eloquent/ModelMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ public static function getMethodReturnType(MethodReturnTypeProviderEvent $event)
return null;
}

/**
* @param FileManipulation[] $file_replacements
*
* @return void
*/
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event)
/** @inheritDoc */
public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event): void
{
$storage = $event->getStorage();
if (
Expand Down
24 changes: 24 additions & 0 deletions src/Handlers/Eloquent/ModelPropertyAccessorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public static function doesPropertyExist(PropertyExistenceProviderEvent $event):
return null;
}

if (self::hasNativeProperty($event->getFqClasslikeName(), $event->getPropertyName())) {
return true;
}

$codebase = $source->getCodebase();

if (self::accessorExists($codebase, $event->getFqClasslikeName(), $event->getPropertyName())) {
Expand All @@ -47,6 +51,10 @@ public static function isPropertyVisible(PropertyVisibilityProviderEvent $event)
return null;
}

if (self::hasNativeProperty($event->getFqClasslikeName(), $event->getPropertyName())) {
return null;
}

$codebase = $event->getSource()->getCodebase();

if (self::accessorExists($codebase, $event->getFqClasslikeName(), $event->getPropertyName())) {
Expand All @@ -64,6 +72,11 @@ public static function getPropertyType(PropertyTypeProviderEvent $event): ?Type\
return null;
}

// skip for real properties like $hidden, $casts
if (self::hasNativeProperty($event->getFqClasslikeName(), $event->getPropertyName())) {
return null;
}

$codebase = $source->getCodebase();
$fq_classlike_name = $event->getFqClasslikeName();
$property_name = $event->getPropertyName();
Expand All @@ -76,6 +89,17 @@ public static function getPropertyType(PropertyTypeProviderEvent $event): ?Type\
return null;
}

private static function hasNativeProperty(string $fqcn, string $property_name): bool
{
try {
new \ReflectionProperty($fqcn, $property_name);
} catch (\ReflectionException $exception) {
return false;
}

return true;
}

private static function accessorExists(Codebase $codebase, string $fq_classlike_name, string $property_name): bool
{
return $codebase->methodExists($fq_classlike_name . '::get' . str_replace('_', '', $property_name) . 'Attribute');
Expand Down
5 changes: 5 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public function scopeActive($query)
{
return $query->where('active', 1);
}

public function getFirstNameUsingLegacyAccessorAttribute(): string
{
return $this->name;
}
}
11 changes: 11 additions & 0 deletions tests/acceptance/EloquentModelPropertyTypes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ Feature: Eloquent Model property types
"""
When I run Psalm
Then I see no errors

Scenario: Legacy Attribute accessor
Given I have the following code
"""
function test(User $user): string
{
return $user->first_name_using_legacy_accessor;
}
"""
When I run Psalm
Then I see no errors