Skip to content

Commit

Permalink
feat: use type from annotation if exists instead of extension
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural committed Apr 12, 2020
1 parent a3e1fc8 commit ec22906
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Properties/ModelPropertyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Iterator;
use PHPStan\Parser\CachedParser;
use PHPStan\PhpDoc\TypeStringResolver;
use PHPStan\Reflection\Annotations\AnnotationsPropertiesClassReflectionExtension;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
Expand All @@ -35,10 +36,14 @@ final class ModelPropertyExtension implements PropertiesClassReflectionExtension
/** @var string */
private $dateClass;

public function __construct(CachedParser $parser, TypeStringResolver $stringResolver)
/** @var AnnotationsPropertiesClassReflectionExtension */
private $annotationExtension;

public function __construct(CachedParser $parser, TypeStringResolver $stringResolver, AnnotationsPropertiesClassReflectionExtension $annotationExtension)
{
$this->parser = $parser;
$this->stringResolver = $stringResolver;
$this->annotationExtension = $annotationExtension;
}

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
Expand All @@ -55,6 +60,10 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
return false;
}

if ($this->annotationExtension->hasProperty($classReflection, $propertyName)) {
return false;
}

if (count($this->tables) === 0) {
$this->initializeTables();
}
Expand Down
12 changes: 11 additions & 1 deletion src/Properties/ModelRelationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Illuminate\Support\Str;
use NunoMaduro\Larastan\Concerns;
use NunoMaduro\Larastan\Types\RelationParserHelper;
use PHPStan\Analyser\OutOfClassScope;
use PHPStan\Reflection\Annotations\AnnotationsPropertiesClassReflectionExtension;
use PHPStan\Reflection\BrokerAwareExtension;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Dummy\DummyPropertyReflection;
Expand All @@ -33,9 +35,13 @@ final class ModelRelationsExtension implements PropertiesClassReflectionExtensio
/** @var RelationParserHelper */
private $relationParserHelper;

public function __construct(RelationParserHelper $relationParserHelper)
/** @var AnnotationsPropertiesClassReflectionExtension */
private $annotationExtension;

public function __construct(RelationParserHelper $relationParserHelper, AnnotationsPropertiesClassReflectionExtension $annotationExtension)
{
$this->relationParserHelper = $relationParserHelper;
$this->annotationExtension = $annotationExtension;
}

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
Expand All @@ -44,6 +50,10 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
return false;
}

if ($this->annotationExtension->hasProperty($classReflection, $propertyName)) {
return false;
}

return $classReflection->hasNativeMethod($propertyName);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Features/Properties/ModelRelationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function testModelWithRelationDefinedInTrait(Account $account): ?User
{
return $account->ownerRelation;
}

public function testRelationCanbeOverridenWithAnnotation(OtherDummyModel $dummyModel): DummyModel
{
return $dummyModel->belongsToRelation;
}
}

class DummyModel extends Model
Expand All @@ -107,6 +112,9 @@ public function relationWithoutReturnType()
}
}

/**
* @property DummyModel $belongsToRelation
*/
class OtherDummyModel extends Model
{
public function belongsToRelation(): BelongsTo
Expand Down

0 comments on commit ec22906

Please sign in to comment.