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
2 changes: 1 addition & 1 deletion lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ public function reset_dirty(): void
/**
* Enables the use of dynamic finders.
*
* @see Relation->__call()
* @see Relation::__call()
*/
public static function __callStatic(string $method, mixed $args): static|null
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ActiveRecord\PhpStan\Model;

use ActiveRecord\Model;
use ActiveRecord\PhpStan\Relation\RelationReflectionHelper;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand Down
3 changes: 2 additions & 1 deletion lib/PhpStan/Model/ModelMethodsClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ActiveRecord\PhpStan\Model;

use ActiveRecord\Model;
use ActiveRecord\Relation;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
Expand All @@ -11,7 +12,7 @@ class ModelMethodsClassReflectionExtension implements MethodsClassReflectionExte
{
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
if ($classReflection->isSubclassOf(Model::class)) {
if ($classReflection->isSubclassOf(Model::class) || $classReflection->is(Relation::class)) {
if (preg_match('/\bfind_by_/', $methodName)) {
return true;
}
Expand Down
17 changes: 0 additions & 17 deletions lib/PhpStan/Model/ModelStaticMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Reflection\MethodReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -126,21 +124,6 @@ public function getVariants(): array
])
)
];
} elseif (str_starts_with($this->name, 'find_all')) {
$parts = SQLBuilder::underscored_string_to_parts(substr($this->name, 9), 0);

return [
new FunctionVariant(
TemplateTypeMap::createEmpty(),
TemplateTypeMap::createEmpty(),
array_fill(0, count($parts), new ModelParameterReflection()),
false,
new ArrayType(
new IntegerType(),
new ObjectType($this->classReflection->getDisplayName()),
)
)
];
} elseif (preg_match('/_set$/', $this->name)) {
return [
new FunctionVariant(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ActiveRecord\PhpStan\Model;
namespace ActiveRecord\PhpStan\Relation;

use ActiveRecord\Relation;
use PhpParser\Node\Expr\MethodCall;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ActiveRecord\PhpStan\Model;
namespace ActiveRecord\PhpStan\Relation;

use PhpParser\Node\Arg;
use PHPStan\Analyser\Scope;
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
-
class: ActiveRecord\PhpStan\Model\RelationDynamicMethodReturnTypeReflection
class: ActiveRecord\PhpStan\Relation\RelationDynamicMethodReturnTypeReflection
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
Expand Down
2 changes: 1 addition & 1 deletion test/ActiveRecordPHPStanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testPhpstan()
// '--debug',
// '-c',
// './phpstan.neon.dist',
// 'lib',
// 'test/phpstan',
// ]
// )
// );
Expand Down
17 changes: 17 additions & 0 deletions test/phpstan/ModelFindByDynamicReturn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is not something we need to execute in tests. It's included
* only as a means to test and aid in the development of dynamic PHPStan
* extensions. If it doesn't emit any errors when you run 'composer stan',
* then everything is working fine.
*
* see lib/PhpStan/ModelStaticMethodReflection.php
*/

use test\models\Book;

$book = Book::find_by_name('Walden');
assert($book instanceof Book);

$book = Book::find_by_name_and_publisher('Walden', 'Random House');
assert(is_null($book));
17 changes: 17 additions & 0 deletions test/phpstan/RelationFindByDynamicReturn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is not something we need to execute in tests. It's included
* only as a means to test and aid in the development of dynamic PHPStan
* extensions. If it doesn't emit any errors when you run 'composer stan',
* then everything is working fine.
*
* see lib/PhpStan/ModelStaticMethodReflection.php
*/

use test\models\Book;

$book = Book::all()->find_by_name('Walden');
assert($book instanceof Book);

$book = Book::all()->find_by_name_and_publisher('Walden', 'Random House');
assert(is_null($book));