Skip to content

Commit

Permalink
Merge pull request #499 from canvural/patch-2
Browse files Browse the repository at this point in the history
Always infer return type of id property on models
  • Loading branch information
canvural committed Mar 20, 2020
2 parents 6d3ce4d + c054642 commit 1055d4a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Properties/ModelPropertyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Type\IntegerType;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
Expand Down Expand Up @@ -58,6 +59,10 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
$this->initializeTables();
}

if ($propertyName === 'id') {
return true;
}

/** @var Model $modelInstance */
$modelInstance = $classReflection->getNativeReflection()->newInstance();
$tableName = $modelInstance->getTable();
Expand All @@ -70,22 +75,32 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
return false;
}

$this->castPropertiesType($modelInstance);

$column = $this->tables[$tableName]->columns[$propertyName];

[$readableType, $writableType] = $this->getReadableAndWritableTypes($column, $modelInstance);

$this->castPropertiesType($modelInstance);

$column->readableType = $readableType;
$column->writeableType = $writableType;

$this->tables[$tableName]->columns[$propertyName] = $column;

return true;
}

public function getProperty(
ClassReflection $classReflection,
string $propertyName
): PropertyReflection {
if ($propertyName === 'id') {
return new ModelProperty(
$classReflection,
new IntegerType(),
new IntegerType()
);
}

/** @var Model $modelInstance */
$modelInstance = $classReflection->getNativeReflection()->newInstance();
$tableName = $modelInstance->getTable();
Expand Down

0 comments on commit 1055d4a

Please sign in to comment.