diff --git a/src/Symfony/Component/TypeInfo/.gitattributes b/src/Symfony/Component/TypeInfo/.gitattributes index 14c3c35940427..413aef4cac05d 100644 --- a/src/Symfony/Component/TypeInfo/.gitattributes +++ b/src/Symfony/Component/TypeInfo/.gitattributes @@ -1,3 +1,4 @@ /Tests export-ignore +/Tests/Fixtures/DummyWithUsesWindowsLineEndings.php text eol=crlf /phpunit.xml.dist export-ignore /.git* export-ignore diff --git a/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithUsesWindowsLineEndings.php b/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithUsesWindowsLineEndings.php new file mode 100644 index 0000000000000..9c7d09be76370 --- /dev/null +++ b/src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyWithUsesWindowsLineEndings.php @@ -0,0 +1,22 @@ +createdAt = $createdAt; + } + + public function getType(): Type + { + throw new \LogicException('Should not be called.'); + } +} diff --git a/src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php b/src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php index 1de82676b0334..c277449659d10 100644 --- a/src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php +++ b/src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php @@ -16,6 +16,7 @@ use Symfony\Component\TypeInfo\Tests\Fixtures\Dummy; use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates; use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithUses; +use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithUsesWindowsLineEndings; use Symfony\Component\TypeInfo\Type; use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory; use Symfony\Component\TypeInfo\TypeResolver\StringTypeResolver; @@ -82,6 +83,24 @@ public function testCollectUses() $this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionParameter([DummyWithUses::class, 'setCreatedAt'], 'createdAt'))->uses); } + public function testCollectUsesWindowsLineEndings() + { + self::assertSame(\count(file(__DIR__.'/../Fixtures/DummyWithUsesWindowsLineEndings.php')), substr_count(file_get_contents(__DIR__.'/../Fixtures/DummyWithUsesWindowsLineEndings.php'), "\r\n")); + + $uses = [ + 'Type' => Type::class, + \DateTimeInterface::class => '\\'.\DateTimeInterface::class, + 'DateTime' => '\\'.\DateTimeImmutable::class, + ]; + + $this->assertSame($uses, $this->typeContextFactory->createFromClassName(DummyWithUsesWindowsLineEndings::class)->uses); + + $this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionClass(DummyWithUsesWindowsLineEndings::class))->uses); + $this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionProperty(DummyWithUsesWindowsLineEndings::class, 'createdAt'))->uses); + $this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionMethod(DummyWithUsesWindowsLineEndings::class, 'setCreatedAt'))->uses); + $this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionParameter([DummyWithUsesWindowsLineEndings::class, 'setCreatedAt'], 'createdAt'))->uses); + } + public function testCollectTemplates() { $this->assertEquals([], $this->typeContextFactory->createFromClassName(Dummy::class)->templates); diff --git a/src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php b/src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php index 8cf405bd76696..5d08c309622d1 100644 --- a/src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php +++ b/src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php @@ -116,7 +116,7 @@ private function collectUses(\ReflectionClass $reflection): array return []; } - if (false === $lines = @file($fileName)) { + if (false === $lines = @file($fileName, \FILE_IGNORE_NEW_LINES)) { throw new RuntimeException(\sprintf('Unable to read file "%s".', $fileName)); } @@ -126,7 +126,7 @@ private function collectUses(\ReflectionClass $reflection): array foreach ($lines as $line) { if (str_starts_with($line, 'use ')) { $inUseSection = true; - $use = explode(' as ', substr($line, 4, -2), 2); + $use = explode(' as ', substr($line, 4, -1), 2); $alias = 1 === \count($use) ? substr($use[0], false !== ($p = strrpos($use[0], '\\')) ? 1 + $p : 0) : $use[1]; $uses[$alias] = $use[0];