--level used: 0Just 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.
// 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
}
------ --------------------------------------------------
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.
------ --------------------------------------------------------------------------
Could you try v0.6.10?
Hi,
Can you also share the code that is giving the error?
It is up there ๐ e.g. class TrustProxies
That's where the constant is defined. But I want to see how its used. For example Http/Requests/CharLoginRequest.php line 30 or Http/Requests/CharNameRequest.php line 29
It is used here.
protected $headers = Request::HEADER_X_FORWARDED_ALL;
I also use "nunomaduro/larastan": "<0.6.11" everywhere as 0.6.11 has something in it.
I also have protected $headers = Request::HEADER_X_FORWARDED_FOR; in TrustProxies.php in my projects, and PHPStan does not complain about it.
So I'm thinking this is an issue with the configuration. Can you also share your phpstan.neon file?
phpstan.neon
includes:
- ./vendor/nunomaduro/larastan/extension.neon
parameters:
paths:
- app
scanFiles:
- _ide_helper.php
- _ide_helper_models.php
level: 0
and the constant being used
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\Character;
class CharLoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'withResponse' => 'nullable',
'charName' => Character::NAME_VALIDATION_RULE, // used here
];
}
}
also im on archlinux using php 7.4.9
Same errors on 0.6.10
Also if i bump the level to 1 it starts erroring about undefined static methods (that are defined) and throws this error at the end (trace with --debug):
Cannot instantiate abstract class Illuminate\Database\Eloquent\Model
at vendor/nunomaduro/larastan/src/Properties/ModelPropertyExtension.php:76
72โ try {
73โ $reflect = new \ReflectionClass($modelName);
74โ
75โ /** @var Model $modelInstance */
โ 76โ $modelInstance = $reflect->newInstanceWithoutConstructor();
77โ
78โ $tableName = $modelInstance->getTable();
79โ } catch (\ReflectionException $e) {
80โ return false;
Have you tried without scanFiles:?
Output with level 1 (no --debug)
------ ------------------------------------------
Line Classes/War.php
------ ------------------------------------------
100 Call to an undefined static method
App\Models\Clan::notifyAllClanMembers().
126 Call to an undefined static method
App\Models\Clan::notifyAllClanMembers().
------ ------------------------------------------
------ ----------------------------------------------------------------------
Line Console/Commands/GetZoneFrames.php
------ ----------------------------------------------------------------------
48 Call to an undefined static method App\Models\Zone::listAvailable().
------ ----------------------------------------------------------------------
------ --------------------------------------------
Line Http/Controllers/BattleController.php
------ --------------------------------------------
752 Call to an undefined static method
App\Models\Battle::createFromConclusion().
------ --------------------------------------------
------ ---------------------------------------------------------------------------
Line Http/Controllers/ClanController.php
------ ---------------------------------------------------------------------------
144 Call to an undefined static method App\Models\UserAction::deployAction().
194 Call to an undefined static method App\Models\UserAction::deployAction().
248 Call to an undefined static method App\Models\UserAction::deployAction().
283 Call to an undefined static method App\Models\UserAction::deployAction().
312 Call to an undefined static method App\Models\UserAction::deployAction().
327 Call to an undefined static method App\Models\UserAction::deployAction().
349 Call to an undefined static method App\Models\UserAction::deployAction().
458 Call to an undefined static method App\Models\UserAction::deployAction().
510 Call to an undefined static method App\Models\UserAction::deployAction().
------ ---------------------------------------------------------------------------
------ -------------------------------------------------------------------
Line Http/Controllers/ConsequencesController.php
------ -------------------------------------------------------------------
352 Call to an undefined static method App\Models\KnownDrop::found().
------ -------------------------------------------------------------------
------ ---------------------------------------------------------------------------
Line Http/Controllers/CosmeticsController.php
------ ---------------------------------------------------------------------------
53 Call to an undefined static method App\Models\UserAction::deployAction().
326 Call to an undefined static method App\Models\Cosmetic::createCosmetic().
------ ---------------------------------------------------------------------------
------ ------------------------------------------------------------------------
Line Http/Controllers/EnemieController.php
------ ------------------------------------------------------------------------
382 Call to an undefined static method App\Models\Enemie::createFromSet().
------ ------------------------------------------------------------------------
------ ---------------------------------------------------------------------------
Line Http/Controllers/ExtrasController.php
------ ---------------------------------------------------------------------------
59 Call to an undefined static method App\Models\UserAction::deployAction().
99 Call to an undefined static method App\Models\UserAction::deployAction().
------ ---------------------------------------------------------------------------
------ ----------------------------------------------------------------------------
Line Http/Controllers/GameController.php
------ ----------------------------------------------------------------------------
195 Call to an undefined static method App\Models\Set::roundSets().
249 Call to an undefined static method
App\Models\HallOfFame::createFromChar().
289 Call to an undefined static method App\Models\Inventory::createFromChar().
------ ----------------------------------------------------------------------------
------ -------------------------------------------------
Line Http/Controllers/InformationController.php
------ -------------------------------------------------
26 Call to an undefined static method
App\Models\Information::variablesFromContent().
59 Call to an undefined static method
App\Models\Information::variablesFromContent().
------ -------------------------------------------------
------ ----------------------------------------------------------------------------
Line Http/Controllers/InventoryController.php
------ ----------------------------------------------------------------------------
94 Call to an undefined static method App\Models\Inventory::createFromChar().
221 Call to an undefined static method App\Models\UserAction::new().
------ ----------------------------------------------------------------------------
------ ----------------------------------------------------------------------
Line Http/Controllers/SetCreationController.php
------ ----------------------------------------------------------------------
107 Call to an undefined static method App\Models\Set::done().
125 Call to an undefined static method App\Models\Set::removeSet().
167 Call to an undefined static method App\Models\Set::createFreshSet().
257 Call to an undefined static method App\Models\Set::removeSet().
274 Call to an undefined static method App\Models\Set::acceptSet().
------ ----------------------------------------------------------------------
------ -------------------------------------------------------------------------
Line Http/Controllers/VaultController.php
------ -------------------------------------------------------------------------
20 Call to an undefined static method App\Models\Vault::createFromAccID().
75 Call to an undefined static method App\Models\Vault::createFromAccID().
85 Call to an undefined static method App\Models\UserAction::new().
101 Call to an undefined static method App\Models\Vault::createFromAccID().
114 Call to an undefined static method App\Models\Vault::createFromAccID().
124 Call to an undefined static method App\Models\UserAction::new().
------ -------------------------------------------------------------------------
------ ----------------------------------------------------------------------
Line Http/Controllers/ZoneController.php
------ ----------------------------------------------------------------------
49 Call to an undefined static method App\Models\Zone::listAvailable().
60 Call to an undefined static method App\Models\Zone::listAvailable().
65 Call to an undefined static method App\Models\Zone::listAvailable().
------ ----------------------------------------------------------------------
------ --------------------------------------------------
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.
------ --------------------------------------------------------------------------
------ ------------------------------------------------------------------
Line Listeners/UserActionEventListener.php
------ ------------------------------------------------------------------
32 Call to an undefined static method App\Models\UserAction::new().
------ ------------------------------------------------------------------
------ ------------------------------------------------------------------------
Line Models/Inventory.php
------ ------------------------------------------------------------------------
48 Call to an undefined static method App\Models\ItemOnSale::charSales().
56 Call to an undefined static method
App\Models\Inventory::isEquipmentSlot().
103 Call to an undefined static method App\Models\Inventory::isSlot().
104 Call to an undefined static method
App\Models\Inventory::isEquipmentSlot().
129 Call to an undefined static method App\Models\Inventory::isSlot().
139 Call to an undefined static method App\Models\Inventory::isSlot().
142 Call to an undefined static method
App\Models\Inventory::markItemsOnSale().
177 Call to an undefined static method
App\Models\Inventory::markItemsOnSale().
192 Call to an undefined static method App\Models\Inventory::isSlot().
193 Call to an undefined static method
App\Models\Inventory::isEquipmentSlot().
300 Call to an undefined static method
App\Models\Inventory::isEquipmentSlot().
------ ------------------------------------------------------------------------
------ -------------------------------------------------------------------------
Line Models/SetInfo.php
------ -------------------------------------------------------------------------
15 Call to an undefined static method App\Models\SetInfo::createFromSet().
------ -------------------------------------------------------------------------
------ ----------------------------------------------------------------------
Line Models/UserAction.php
------ ----------------------------------------------------------------------
25 Call to an undefined static method App\Models\UserAction::new().
40 Call to an undefined static method App\Models\UserAction::factory().
------ ----------------------------------------------------------------------
------ ---------------------------------------------------------------------
Line Services/CharCreationService.php
------ ---------------------------------------------------------------------
39 Call to an undefined static method App\Models\Character::factory().
------ ---------------------------------------------------------------------
------ -----------------------------------------------------------------------
Line Services/CommerceService.php
------ -----------------------------------------------------------------------
104 Call to an undefined static method App\Models\ItemOnSale::findSale().
138 Call to an undefined static method App\Models\ItemOnSale::new().
------ -----------------------------------------------------------------------
------ -------------------------------------------------------------------------
Line Services/ExperienceService.php
------ -------------------------------------------------------------------------
103 Call to an undefined static method App\Models\Character::getTopLevel().
------ -------------------------------------------------------------------------
------ -------------------------------------------------------------------------
Line Services/PublicMarketService.php
------ -------------------------------------------------------------------------
44 Call to an undefined static method App\Models\Character::getTopLevel().
64 Call to an undefined static method App\Models\Character::getTopLevel().
------ -------------------------------------------------------------------------
-- ---------------------------------------------------------------------
Error
-- ---------------------------------------------------------------------
Internal error: Internal error: Cannot instantiate abstract class
Illuminate\Database\Eloquent\Model in file
/home/lordmanuel/Projects/SuperFighterV2/app/Models/Zone.php
Run PHPStan with --debug option and post the stack trace to:
https://github.com/phpstan/phpstan/issues/new
Internal error: Internal error: Cannot instantiate abstract class
Illuminate\Database\Eloquent\Model in file
/home/lordmanuel/Projects/SuperFighterV2/app/Models/Set.php
Run PHPStan with --debug option and post the stack trace to:
https://github.com/phpstan/phpstan/issues/new
Internal error: Internal error: Cannot instantiate abstract class
Illuminate\Database\Eloquent\Model in file
/home/lordmanuel/Projects/SuperFighterV2/app/Models/ClanVault.php
Run PHPStan with --debug option and post the stack trace to:
https://github.com/phpstan/phpstan/issues/new
Internal error: Internal error: Cannot instantiate abstract class
Illuminate\Database\Eloquent\Model in file
/home/lordmanuel/Projects/SuperFighterV2/app/Models/Character.php
Run PHPStan with --debug option and post the stack trace to:
https://github.com/phpstan/phpstan/issues/new
Internal error: Internal error: Cannot instantiate abstract class
Illuminate\Database\Eloquent\Model in file
/home/lordmanuel/Projects/SuperFighterV2/app/Models/Information.php
Run PHPStan with --debug option and post the stack trace to:
https://github.com/phpstan/phpstan/issues/new
Internal error: Internal error: Cannot instantiate abstract class
Illuminate\Database\Eloquent\Model in file
/home/lordmanuel/Projects/SuperFighterV2/app/Models/User.php
Run PHPStan with --debug option and post the stack trace to:
https://github.com/phpstan/phpstan/issues/new
Internal error: Internal error: Cannot instantiate abstract class
Illuminate\Database\Eloquent\Model in file
/home/lordmanuel/Projects/SuperFighterV2/app/Models/Clan.php
Run PHPStan with --debug option and post the stack trace to:
https://github.com/phpstan/phpstan/issues/new
Child process error (exit code 1):
Child process error (exit code 1):
-- ---------------------------------------------------------------------
[ERROR] Found 75 errors
I know i shouldn't be using so many static methods, im actually working on a big refactor, that's why i installed larastan in the first place.
without scanFiles it works okay in level: 0 and does not throw the error on level: 1
but it shows every relationship as undefined property.
It also still says the
------ -------------------------------------------------------------------
Line Models/Character.php
------ -------------------------------------------------------------------
180 Access to an undefined property App\Models\Character::$user.
321 Access to an undefined property App\Models\Character::$inventory.
338 Access to an undefined property App\Models\Character::$set.
362 Access to an undefined property App\Models\Character::$user.
363 Access to an undefined property App\Models\Character::$user.
------ -------------------------------------------------------------------
------ ----------------------------------------------------------------
Line Models/Clan.php
------ ----------------------------------------------------------------
91 Access to an undefined property App\Models\Clan::$userMembers.
98 Access to an undefined property App\Models\Clan::$base.
172 Access to an undefined property App\Models\Clan::$leader.
201 Access to an undefined property App\Models\Clan::$vault.
247 Access to an undefined property App\Models\Clan::$userMembers.
------ ----------------------------------------------------------------
------ --------------------------------------------------------------
Line Models/ClanVault.php
------ --------------------------------------------------------------
36 Access to an undefined property App\Models\ClanVault::$clan.
56 Access to an undefined property App\Models\ClanVault::$clan.
------ --------------------------------------------------------------
------ -------------------------------------------------------------------------
Line Models/Information.php
------ -------------------------------------------------------------------------
31 Access to an undefined property App\Models\Information::$parsedContent.
37 Access to an undefined property App\Models\Information::$varValues.
------ -------------------------------------------------------------------------
------ ---------------------------------------------------------
Line Models/Set.php
------ ---------------------------------------------------------
54 Access to an undefined property App\Models\Set::$items.
219 Access to an undefined property App\Models\Set::$items.
------ ---------------------------------------------------------
------ ----------------------------------------------------------
Line Models/User.php
------ ----------------------------------------------------------
76 Access to an undefined property App\Models\User::$chars.
98 Access to an undefined property App\Models\User::$chars.
110 Access to an undefined property App\Models\User::$chars.
118 Access to an undefined property App\Models\User::$bank.
------ ----------------------------------------------------------
------ ----------------------------------------------------------------------
Line Models/UserAction.php
------ ----------------------------------------------------------------------
40 Call to an undefined static method App\Models\UserAction::factory().
------ ----------------------------------------------------------------------
------ ---------------------------------------------------------------
Line Models/Zone.php
------ ---------------------------------------------------------------
58 Access to an undefined property App\Models\Zone::$warReports.
------ ---------------------------------------------------------------
------ ---------------------------------------------------------------------
Line Services/CharCreationService.php
------ ---------------------------------------------------------------------
39 Call to an undefined static method App\Models\Character::factory().
------ ---------------------------------------------------------------------
[ERROR] Found 23 errors
but it shows every relationship as undefined property
Try adding proper migrations that Larastan understands.
Using
scanFiles:
- _ide_helper.php
- _ide_helper_models.php
together with Larastan causes the issues I think. You have to choose one I think.
About properties, you can follow the advice from @szepeviktor or write your properties into your model files with ide-helper.
Should i remake my whole database schema? what about the problem with factories?
Early migrations are kind of junky since this project was my first laravel project and i learnt as i progressed on it but it is not a small project and remaking the whole schema would be a big headache.
I don't think this should be closed as the problem with factories will still persist even if i rewrite all the migrations.
What do you mean remaking the schema? You don't have to change your schema. Do you have database migrations? Larastan will scan them automatically, if it exists.
About factories, there is an open feature request about it: #661 They are not supported yet by Larastan
Yes, i have database migrations but i should still rewrite every migration where there is a relationship defined right? where i should use ->foreingId().
I'll try telling ide_helper to write the docblocks inside the model files as i think that should fix the problem.
Thanks for the help!
Sorry for my bad english and greetings from Argentina!
Yes, i have database migrations but i should still rewrite every migration where there is a relationship defined right? where i should use ->foreingId().
No, you don't need to that. Recognizing relationships as properties does not require the migration files at all. If you have a method in your model that returns a relation, it should be recognized by Larastan as a property when you try to access it.
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.
You need to define the return type. Either with public function chars(): HasMany or /** @return HasMany */ above the method definition.
Great! that fixes it! thanks for the help!
@manuelmaceira No problem!
Most helpful comment
You need to define the return type. Either with
public function chars(): HasManyor/** @return HasMany */above the method definition.