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
10 changes: 10 additions & 0 deletions src/Method/AssociationTableMixinClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,19 @@
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
// magic findBy* method
if ($classReflection->isSubclassOf(Table::class) && preg_match('/^find(?:\w+)?By/', $methodName) > 0) {

Check failure on line 52 in src/Method/AssociationTableMixinClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to deprecated method isSubclassOf() of class PHPStan\Reflection\ClassReflection: Use isSubclassOfClass instead.
return true;
}

if (!$classReflection->isSubclassOf(Association::class)) {

Check failure on line 56 in src/Method/AssociationTableMixinClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to deprecated method isSubclassOf() of class PHPStan\Reflection\ClassReflection: Use isSubclassOfClass instead.
return false;
}

// magic findBy* method on Association
if (preg_match('/^find(?:\w+)?By/', $methodName) > 0) {
return true;
}

return $this->getTableReflection()->hasMethod($methodName);
}

Expand All @@ -68,10 +73,15 @@
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
// magic findBy* method
if ($classReflection->isSubclassOf(Table::class) && preg_match('/^find(?:\w+)?By/', $methodName) > 0) {

Check failure on line 76 in src/Method/AssociationTableMixinClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to deprecated method isSubclassOf() of class PHPStan\Reflection\ClassReflection: Use isSubclassOfClass instead.
return new TableFindByPropertyMethodReflection($methodName, $classReflection);
}

// magic findBy* method on Association
if ($classReflection->isSubclassOf(Association::class) && preg_match('/^find(?:\w+)?By/', $methodName) > 0) {

Check failure on line 81 in src/Method/AssociationTableMixinClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to deprecated method isSubclassOf() of class PHPStan\Reflection\ClassReflection: Use isSubclassOfClass instead.
return new TableFindByPropertyMethodReflection($methodName, $this->getTableReflection());
}

return $this->getTableReflection()->getNativeMethod($methodName);
}

Expand All @@ -82,11 +92,11 @@
*/
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
if (!$classReflection->isSubclassOf(Association::class)) {

Check failure on line 95 in src/Method/AssociationTableMixinClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to deprecated method isSubclassOf() of class PHPStan\Reflection\ClassReflection: Use isSubclassOfClass instead.
return false;
}

return $this->getTableReflection()->hasProperty($propertyName);

Check failure on line 99 in src/Method/AssociationTableMixinClassReflectionExtension.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to deprecated method hasProperty() of class PHPStan\Reflection\ClassReflection: Use hasInstanceProperty or hasStaticProperty instead
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/test_app/Model/Table/NotesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public function warning(): array
$this->MyUsers->logLastLogin($user);
$article = $this->MyUsers->Articles->newSample();
$article->id = '002';
// Test magic findBy methods on association chains (fixes issue #51)
$articleQuery = $this->MyUsers->Articles->findByTitle('Test Title');
$foundArticle = $articleQuery->first();
// Test findBy with And operator
$articleAndQuery = $this->MyUsers->Articles->findByTitleAndActive('Test', true);
// Test findBy with Or operator
$articleOrQuery = $this->MyUsers->Articles->findByTitleOrActive('Test', true);
$entity = $this->get(10, cache: 'my_cache');
if ($entity->note === 'Test') {
$entity = $this->newEmptyEntity();
Expand Down
3 changes: 3 additions & 0 deletions tests/test_app/Model/Table/UsersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ public function logLastLogin(User $user): User
*/
public function blockOld(): void
{
// Test magic findBy methods on association (issue #51)
$articleQuery = $this->Articles->findByTitle('Test');
$article = $articleQuery->first();
}
}
Loading