larastan: Access to undefined constant all over the proyect

  • Larastan Version: 0.6.11
  • --level used: 0

Description

Just installed larastan in a just upgraded proyect (was laravel 7 but now it’s laravel 8) and phpstan is giving me errors about undefined class constants that are already defined.

In phpstan playground it’s working as it should.

Laravel code where the issue was found

// In app/Http/Middleware/TrustProxies.php
<?php

namespace App\Http\Middleware;

use Fideloper\Proxy\TrustProxies as Middleware;
use Illuminate\Http\Request;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array|string|null
     */
    protected $proxies;

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL; // defined here
}

// And in Character.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Character extends Model
{
	public const NAME_VALIDATION_RULE = [ // defined here
		'required',
		'exists:chars,nombre',
		'max:16',
	];

	// extra code removed
}


Output

 ------ --------------------------------------------------
  Line   Http/Middleware/TrustProxies.php
 ------ --------------------------------------------------
  22     Access to undefined constant
         Illuminate\Http\Request::HEADER_X_FORWARDED_ALL.
 ------ --------------------------------------------------

 ------ --------------------------------------------------------------------------
  Line   Http/Requests/CharLoginRequest.php
 ------ --------------------------------------------------------------------------
  30     Access to undefined constant App\Models\Character::NAME_VALIDATION_RULE.
 ------ --------------------------------------------------------------------------

 ------ --------------------------------------------------------------------------
  Line   Http/Requests/CharNameRequest.php
 ------ --------------------------------------------------------------------------
  29     Access to undefined constant App\Models\Character::NAME_VALIDATION_RULE.
 ------ --------------------------------------------------------------------------

 ------ --------------------------------------------------------------------------
  Line   Http/Requests/DistributeStatsRequest.php
 ------ --------------------------------------------------------------------------
  30     Access to undefined constant App\Models\Character::NAME_VALIDATION_RULE.
 ------ --------------------------------------------------------------------------

 ------ --------------------------------------------------------------------------
  Line   Http/Requests/ResetStatsRequest.php
 ------ --------------------------------------------------------------------------
  30     Access to undefined constant App\Models\Character::NAME_VALIDATION_RULE.
 ------ --------------------------------------------------------------------------

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 25

Most upvoted comments

You need to define the return type. Either with public function chars(): HasMany or /** @return HasMany */ above the method definition.

About factories, there is an open feature request about it: #661 They are not supported yet by Larastan

Great! that fixes it! thanks for the help!

I have methods that return relations but larastan is not recognizing them.

 ------ ----------------------------------------------------------
  Line   Models/User.php
 ------ ----------------------------------------------------------
  76     Access to an undefined property App\Models\User::$chars.
  98     Access to an undefined property App\Models\User::$chars.
 ------ ----------------------------------------------------------
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

use App\Models\Character;

class User extends Model
{
	public function chars() 
	{
		return $this->hasMany(Character::class, 'accID', 'id');
	}
	// extra code removed

}

If i let ide-helper write the docblocks on the model files it works fine but i would prefer it if i could keep those at _ide_helper_models.php.

Have you tried without scanFiles:?