-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyInfo] Fix phpstan extractor issues #44578
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,10 @@ | |
namespace Symfony\Component\Serializer\Tests\Normalizer; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use PHPStan\PhpDocParser\Parser\PhpDocParser; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; | ||
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor; | ||
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; | ||
use Symfony\Component\PropertyInfo\PropertyInfoExtractor; | ||
use Symfony\Component\Serializer\Exception\LogicException; | ||
|
@@ -721,6 +723,22 @@ public function testAcceptJsonNumber() | |
$this->assertSame(10.0, $serializer->denormalize(['number' => 10], JsonNumber::class, 'jsonld')->number); | ||
} | ||
|
||
public function testDoesntHaveIssuesWithUnionConstTypes() | ||
{ | ||
if (!class_exists(PhpStanExtractor::class) || !class_exists(PhpDocParser::class)) { | ||
$this->markTestSkipped('phpstan/phpdoc-parser required for this test'); | ||
} | ||
|
||
$extractor = new PropertyInfoExtractor([], [new PhpStanExtractor(), new PhpDocExtractor(), new ReflectionExtractor()]); | ||
$normalizer = new ObjectNormalizer(null, null, null, $extractor); | ||
$serializer = new Serializer([new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer]); | ||
|
||
$this->assertSame('bar', $serializer->denormalize(['foo' => 'bar'], \get_class(new class() { | ||
/** @var self::*|null */ | ||
public $foo; | ||
}))->foo); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was passing before the change on PropertyInfo, isn't it? (I'm saying that because deps=low tests still pass) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I wanted to have some "integration" test with serializer to demonstrate the problem, so to increase coverage yes. We didn't have any tests in serializer that do use PhpStanExtractor. This test doesn't pass without my changes in propertyinfo |
||
|
||
public function testExtractAttributesRespectsFormat() | ||
{ | ||
$normalizer = new FormatAndContextAwareNormalizer(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just wondering: why do we want to add this to 5.4 while the other pseudo-types are added on 6.1 (see #44451)?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think root of the problem is fallback case here on L182. If PhpStanTypeHelper doesn't recognize the type, it returns object. This is what causes issues during 5.3 -> 5.4 upgrade for my second use case. Potentially we could change that to return empty array instead, but that's very aggressive change compared to just adding support for non-empty-array (it might break various cases of
@return Foo
,@return Foo<bar>
and so on. Alternatively, in my opinion backporting changes from #44451 to 5.4 would be fine too. All of those cases that were added there were most likely working fine in 5.3, but got broken in 5.4.non-empty-array
just happens to be more often used than the rest of them and that's what blowed up in my projects.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that's exactly why I posted #44451 (review)
@derrabus WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't add support for that type here. If we get a proper fallback if the PHPStan extractor reported
null
here, that's the way to go imho.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well then any idea how to properly make a fallback here? We need a mechanism how to detect object, instead of falling back to object on everything that's not recognized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, your two answers were not displayed when I posted mine which obviously does not make sense in that context. 🙈
I agree that in that particular case, active support for
non-empty-array
(and possiblynon-empty-list
as well) would be the least invasive fix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added non-empty-list :)