Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PHPStan PHPDoc parser 2.0 #1572

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"doctrine/instantiator": "^1.3.1 || ^2.0",
"doctrine/lexer": "^2.0 || ^3.0",
"jms/metadata": "^2.6",
"phpstan/phpdoc-parser": "^1.20"
Copy link
Collaborator

@scyzoryck scyzoryck Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if we can support both - 1.X & 2.x. Without it people that are using slevomat/coding-standard or rector will be not able to use new version of serializer.
At the moment it is a blocker for me as we need to release some bug for the Union types - but I'm fine with releasing it with only 2.x support after releasing bug fix release - soo we would need to wait ~1 week to merge this one.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory we can have a hacky if(class_exist(ParserConfig::class)). It will look terrible but most probably it will do the job :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do that indeed :)

"phpstan/phpdoc-parser": "^1.20 || ^2.0"
},
"suggest": {
"doctrine/collections": "Required if you like to use doctrine collection types as ArrayCollection.",
Expand All @@ -37,17 +37,18 @@
"require-dev": {
"ext-pdo_sqlite": "*",
"doctrine/annotations": "^1.14 || ^2.0",
"slevomat/coding-standard": "dev-master#f2cc4c553eae68772624ffd7dd99022343b69c31 as 8.11.9999",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"doctrine/coding-standard": "^12.0",
"doctrine/orm": "^2.14 || ^3.0",
"doctrine/persistence": "^2.5.2 || ^3.0",
"doctrine/phpcr-odm": "^1.5.2 || ^2.0",
"jackalope/jackalope-doctrine-dbal": "^1.3",
"ocramius/proxy-manager": "^1.0 || ^2.0",
"phpbench/phpbench": "^1.0",
"phpstan/phpstan": "^1.10.57",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^9.0 || ^10.0 || ^11.0",
"psr/container": "^1.0 || ^2.0",
"rector/rector": "^1.0.0",
"rector/rector": "dev-main#49f448773bbd6b81a57de15a92f25519b241ea20 as 2.0",
"symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0",
"symfony/expression-language": "^5.4 || ^6.0 || ^7.0",
"symfony/filesystem": "^5.4 || ^6.0 || ^7.0",
Expand Down
8 changes: 5 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ parameters:
- '#^Property JMS\\Serializer\\Handler\\FormErrorHandler\:\:\$translator has unknown class Symfony\\Component\\Translation\\TranslatorInterface as its type\.$#'
- '#^Cannot call method appendChild\(\) on null\.$#'
- '#^Call to an undefined method JMS\\Serializer\\VisitorInterface\:\:setData\(\)\.$#'
- '#^Property JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\CollectionOfNotExistingClasses\:\:\$productIds has unknown class JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\NotExistingClass as its type\.$#'
- '#^Call to method expects\(\) on an unknown class Symfony\\Component\\Translation\\TranslatorInterface\.$#'
- '#^Call to an undefined method JMS\\Serializer\\VisitorInterface\:\:hasData\(\)\.$#'
- '#^Property JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\CollectionOfClassesWithFullNamespacePath\:\:\$productIds has unknown class JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\Product as its type\.$#'
- '#^Property JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\CollectionOfInterfacesWithFullNamespacePath\:\:\$productColors has unknown class JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\JMS\\Serializer\\Tests\\Fixtures\\DocBlockType\\Collection\\Details\\ProductColor as its type\.$#'
- '#^Method JMS\\Serializer\\GraphNavigator\\DeserializationGraphNavigator\:\:resolveMetadata\(\) should return JMS\\Serializer\\Metadata\\ClassMetadata\|null#'
- '#^ArrayObject<\*NEVER\*, \*NEVER\*> does not accept list<string\|null>\.#'
- '#^ArrayObject<\*NEVER\*, \*NEVER\*> does not accept array<string, ArrayObject>\.#'
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
excludePaths:
- tests/Fixtures/*
reportUnmatchedIgnoredErrors: false
1 change: 0 additions & 1 deletion phpstan/no-typed-prop.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ parameters:
- %currentWorkingDirectory%/tests/Fixtures/DocBlockType/Collection/ConstructorPropertyPromotion.php
- %currentWorkingDirectory%/tests/Fixtures/DocBlockType/Collection/ConstructorPropertyPromotionWithoutDocblock.php
- %currentWorkingDirectory%/tests/Fixtures/DocBlockType/Collection/ConstructorPropertyPromotionWithScalar.php
- %currentWorkingDirectory%/tests/Serializer/BaseSerializationTest.php
1 change: 1 addition & 0 deletions phpstan/no-unions.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
parameters:
excludePaths:
- %currentWorkingDirectory%/src/Handler/UnionHandler.php
- %currentWorkingDirectory%/tests/Fixtures/TypedProperties/ComplexDiscriminatedUnion.php
1 change: 0 additions & 1 deletion src/Handler/FormErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ private function getErrorMessage(FormError $error): ?string

private function convertFormToArray(SerializationVisitorInterface $visitor, FormInterface $data): \ArrayObject
{
/** @var \ArrayObject{errors?:array<string>,children?:array<string,\ArrayObject>} $form */
$form = new \ArrayObject();
$errors = [];
foreach ($data->getErrors() as $error) {
Expand Down
23 changes: 19 additions & 4 deletions src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;

/**
* @internal
Expand Down Expand Up @@ -47,11 +48,25 @@ final class DocBlockTypeResolver

public function __construct()
{
$constExprParser = new ConstExprParser();
$typeParser = new TypeParser($constExprParser);
// PHPStan PHPDoc Parser 2
if (class_exists(ParserConfig::class)) {
$config = new ParserConfig(['lines' => true, 'indexes' => true]);

$this->phpDocParser = new PhpDocParser($typeParser, $constExprParser);
$this->lexer = new Lexer();
$constExprParser = new ConstExprParser($config);
$typeParser = new TypeParser($config, $constExprParser);

$this->phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
$this->lexer = new Lexer($config);
} else {
// @phpstan-ignore arguments.count
$constExprParser = new ConstExprParser();
// @phpstan-ignore arguments.count
$typeParser = new TypeParser($constExprParser);
// @phpstan-ignore arguments.count
$this->phpDocParser = new PhpDocParser($typeParser, $constExprParser);
// @phpstan-ignore arguments.count
$this->lexer = new Lexer();
}
}

/**
Expand Down
Loading