phpDocumentor/TypeResolver

BUG: Wrong argument type when use phpstan/phpdoc-parser:^2.0

Closed this issue · 9 comments

{
  "errors": true,
  "message": "PHPStan\\PhpDocParser\\Parser\\ConstExprParser::__construct(): Argument #1 ($unescapeStrings) must be of type bool, PHPStan\\PhpDocParser\\ParserConfig given, called in /var/www/html/vendor/phpdocumentor/type-resolver/src/TypeResolver.php on line 195"
}

According to the error message you are not using version 2 of the phpstan/phpdoc-parser package. Could it be that you have both (1 and 2) versions of it installed and that the autoloader mixes both versions? Can you provide more context information about when this happens?

No, I have only 1 version (2.0) installed, I use composer as a package manager. After the last update I have phpstan/phpdoc-parser version 2.0, before that I had version 1.33 and everything worked fine.

After the update, this error started to occur on some API endpoints. It can be solved by reverting back to 1.33 or installing packages with --no-dev

I don’t see how that could happen. Can you provide a small example application that allows to reproduce this?

I can't do it now, due to lack of time, but I will try as soon as I have the opportunity

Otherwise if you want to debug this yourself you can use PHP's reflection API to debug where the ConstExprParser class is located that your application seems to use.

I was able to reproduce this on a simple example of Larevel (here I got an error)
Steps to reproduce,

  • install the base deployment for Larvel (10/11)
  • install the spatie/laravel-data package (composer r spatie/laravel-data) (v4.11)
  • create a regular object of class DTO
<?php

declare(strict_types=1);

namespace App\Data;

use Spatie\LaravelData\Data;

final class TestDTO extends Data
{
    public function __construct(
        public string $test = '',
    ) {}
}
  • try to create this object in a regular routine:
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;

Route::get('/test', function () {

    $dto = \App\Data\TestDTO::from([
        'test' => 'test',
    ]);

    return view('welcome');
});
  • get a similar error report
[2024-11-20 18:21:29] dev.ERROR: PHPStan\PhpDocParser\Parser\ConstExprParser::__construct(): Argument #1 ($unescapeStrings) must be of type bool, PHPStan\PhpDocParser\ParserConfig given, called in /var/www/html/vendor/phpdocumentor/type-resolver/src/TypeResolver.php on line 195 {"exception":"[object] (TypeError(code: 0): PHPStan\\PhpDocParser\\Parser\\ConstExprParser::__construct(): Argument #1 ($unescapeStrings) must be of type bool, PHPStan\\PhpDocParser\\ParserConfig given, called in /var/www/html/vendor/phpdocumentor/type-resolver/src/TypeResolver.php on line 195 at /var/www/html/vendor/rector/rector/vendor/phpstan/phpdoc-parser/src/Parser/ConstExprParser.php:26)
[stacktrace]
#0 /var/www/html/vendor/phpdocumentor/type-resolver/src/TypeResolver.php(195): PHPStan\\PhpDocParser\\Parser\\ConstExprParser->__construct()
#1 [internal function]: phpDocumentor\\Reflection\\TypeResolver->__construct()
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(985): ReflectionClass->newInstanceArgs()
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(815): Illuminate\\Container\\Container->build()
#4 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1048): Illuminate\\Container\\Container->resolve()
#5 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(751): Illuminate\\Foundation\\Application->resolve()
#6 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1030): Illuminate\\Container\\Container->make()
#7 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(1114): Illuminate\\Foundation\\Application->make()
#8 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(1024): Illuminate\\Container\\Container->resolveClass()
#9 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(975): Illuminate\\Container\\Container->resolveDependencies()
#10 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(815): Illuminate\\Container\\Container->build()
#11 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1048): Illuminate\\Container\\Container->resolve()
#12 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(751): Illuminate\\Foundation\\Application->resolve()
#13 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1030): Illuminate\\Container\\Container->make()
#14 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(1114): Illuminate\\Foundation\\Application->make()
#15 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(1024): Illuminate\\Container\\Container->resolveClass()
#16 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(975): Illuminate\\Container\\Container->resolveDependencies()
#17 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(815): Illuminate\\Container\\Container->build()
#18 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1048): Illuminate\\Container\\Container->resolve()
#19 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(751): Illuminate\\Foundation\\Application->resolve()
#20 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1030): Illuminate\\Container\\Container->make()
#21 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(1114): Illuminate\\Foundation\\Application->make()
#22 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(1024): Illuminate\\Container\\Container->resolveClass()
#23 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(975): Illuminate\\Container\\Container->resolveDependencies()
#24 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(815): Illuminate\\Container\\Container->build()
#25 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1048): Illuminate\\Container\\Container->resolve()
#26 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(751): Illuminate\\Foundation\\Application->resolve()
#27 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1030): Illuminate\\Container\\Container->make()
#28 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(1114): Illuminate\\Foundation\\Application->make()
#29 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(1024): Illuminate\\Container\\Container->resolveClass()
#30 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(975): Illuminate\\Container\\Container->resolveDependencies()
#31 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(815): Illuminate\\Container\\Container->build()
#32 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1048): Illuminate\\Container\\Container->resolve()
#33 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(751): Illuminate\\Foundation\\Application->resolve()
#34 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1030): Illuminate\\Container\\Container->make()
#35 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(124): Illuminate\\Foundation\\Application->make()
#36 /var/www/html/vendor/spatie/laravel-data/src/Support/DataContainer.php(87): app()
#37 /var/www/html/vendor/spatie/laravel-data/src/Support/DataConfig.php(62): Spatie\\LaravelData\\Support\\DataContainer->dataClassFactory()
#38 /var/www/html/vendor/spatie/laravel-data/src/Resolvers/DataFromSomethingResolver.php(73): Spatie\\LaravelData\\Support\\DataConfig->getDataClass()
#39 /var/www/html/vendor/spatie/laravel-data/src/Resolvers/DataFromSomethingResolver.php(33): Spatie\\LaravelData\\Resolvers\\DataFromSomethingResolver->createFromCustomCreationMethod()
#40 /var/www/html/vendor/spatie/laravel-data/src/Support/Creation/CreationContextFactory.php(186): Spatie\\LaravelData\\Resolvers\\DataFromSomethingResolver->execute()
#41 /var/www/html/vendor/spatie/laravel-data/src/Concerns/BaseData.php(46): Spatie\\LaravelData\\Support\\Creation\\CreationContextFactory->from()
#42 /var/www/html/routes/web.php(14): Spatie\\LaravelData\\Data::from()
...

if Downgrading phpstan/phpdoc-parser (2.0.0 => 1.33.0) this error will not occur

Can you create a repository with all the steps already done that are needed to get there? I have no knowledge about Laravel.

It looks to me like somehow your setup is broken, the reported error could be caused by any other dependency which is pulling in the older version via some hack. I have seen these kind of errors before when a project had phar files which were included into the autoloading.

I don't know exactly how for example pest does do it's autoloading, but that could be involved, as this package is a dependency of phpunit indirect. It would be useful to debug the exact loaded class using xdebug for example.

When you check the phpstan library involved you will notice that the error class has the config object as a constructor argument. And our pipelines do check the compatibility of all allowed versions.

Please do also make sure to check with composer what has been installed.

Okay, I figured out the problem. I connected xdebug and saw an interesting detail. The rector/rector package contains in its code a folder vendor in which the old version of phpstan/phpdoc-parser is stored. Because of this, I thought I had the correct version installed (I checked with composer). But debug showed that I was using a version from a different location than I needed.

Updating rector to the dev version solved the problem (they have already updated the version to 2).