Add support for reading public constants while parsing migrations
Author: patrick-lebCreated Nov 17, 2024Updated Sep 1, 2026
Labelsenhancement
- Larastan Version: 3.0.0
- Laravel Version: 11.32.0
Description
I'm having the problem that has been reported a few times over the years it seems.
Line Models/Compensation.php
------ --------------------------------------------------------------------------------------
66 Access to an undefined property App\Models\Compensation::$period.
property.notFound
Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
67 Access to an undefined property App\Models\Compensation::$amount.
property.notFound
Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
68 Access to an undefined property App\Models\Compensation::$amount.
[...] Laravel code where the issue was found
class abbreviated for simplicity and sensitivty
<?php
declare(strict_types=1);
namespace App\Models;
class Compensation extends Model
{
use HasFactory;
use SoftDeletes;
public const ATTR_AMOUNT = 'amount';
protected $casts = [
'id' => 'integer',
'amount' => 'float',
];
protected $fillable = [
'amount',
];
public function toHourlyRate(float $weeklyHours): float
{
return match ($this->{self::ATTR_PERIOD}) {
CompensationPeriod::HOURLY => $this->{self::ATTR_AMOUNT},
CompensationPeriod::DAILY => $this->{self::ATTR_AMOUNT} * 5 / $weeklyHours,
CompensationPeriod::WEEKLY => $this->{self::ATTR_AMOUNT} / $weeklyHours,
CompensationPeriod::BIWEEKLY => $this->{self::ATTR_AMOUNT} / ($weeklyHours * 2),
CompensationPeriod::BIMONTHLY => $this->{self::ATTR_AMOUNT} * 24 / ($weeklyHours * 52),
CompensationPeriod::MONTHLY => $this->{self::ATTR_AMOUNT} * 12 / ($weeklyHours * 52),
CompensationPeriod::QUARTERLY => $this->{self::ATTR_AMOUNT} * 4 / ($weeklyHours * 52),
CompensationPeriod::SEMIANNUALLY => $this->{self::ATTR_AMOUNT} * 2 / ($weeklyHours * 52),
CompensationPeriod::ANNUALLY => $this->{self::ATTR_AMOUNT} / ($weeklyHours * 52),
default => throw new \InvalidArgumentException(__('Invalid compensation period')),
};
Migration file, located in the default path database/migrations
public function up(): void
{
Schema::create('compensation', function (Blueprint $table) {
$table->id();
[...]
$table->decimal(Compensation::ATTR_AMOUNT, 10, 2);
$table->timestamps();
$table->softDeletes();
});
}I thought maybe there was a circular reference between the model and migration, but even with replacing the column name with the string, it still throws the same error.
It's not limited to INSIDE the class either, eg:
------ ---------------------------------------------------------------------------------------------------
Line Observers/CompensationObserver.php
------ ---------------------------------------------------------------------------------------------------
46 Access to an undefined property App\Models\Compensation::$amount.
property.notFound
Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property I was hoping to fix this without adding a docbloc since, from my understanding, larastan is supposed to scan the migration file to understand the properties. I hope this is a good issue, if you need more clarification I'll be happy to provide!
Source: larastan/larastan