Skip to content

Commit cf0e627

Browse files
committed
bug #60909 [TypeInfo] use an EOL-agnostic approach to parse class uses (xabbuh)
This PR was merged into the 7.2 branch. Discussion ---------- [TypeInfo] use an EOL-agnostic approach to parse class uses | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #60906 | License | MIT Commits ------- d7eedcf use an EOL-agnostic approach to parse class uses
2 parents 40f9668 + d7eedcf commit cf0e627

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/Tests export-ignore
2+
/Tests/Fixtures/DummyWithUsesWindowsLineEndings.php text eol=crlf
23
/phpunit.xml.dist export-ignore
34
/.git* export-ignore
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Symfony\Component\TypeInfo\Tests\Fixtures;
4+
5+
use Symfony\Component\TypeInfo\Type;
6+
use \DateTimeInterface;
7+
use \DateTimeImmutable as DateTime;
8+
9+
final class DummyWithUsesWindowsLineEndings
10+
{
11+
private DateTimeInterface $createdAt;
12+
13+
public function setCreatedAt(DateTime $createdAt): void
14+
{
15+
$this->createdAt = $createdAt;
16+
}
17+
18+
public function getType(): Type
19+
{
20+
throw new \LogicException('Should not be called.');
21+
}
22+
}

src/Symfony/Component/TypeInfo/Tests/TypeContext/TypeContextFactoryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\TypeInfo\Tests\Fixtures\Dummy;
1717
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates;
1818
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithUses;
19+
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithUsesWindowsLineEndings;
1920
use Symfony\Component\TypeInfo\Type;
2021
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory;
2122
use Symfony\Component\TypeInfo\TypeResolver\StringTypeResolver;
@@ -82,6 +83,24 @@ public function testCollectUses()
8283
$this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionParameter([DummyWithUses::class, 'setCreatedAt'], 'createdAt'))->uses);
8384
}
8485

86+
public function testCollectUsesWindowsLineEndings()
87+
{
88+
self::assertSame(\count(file(__DIR__.'/../Fixtures/DummyWithUsesWindowsLineEndings.php')), substr_count(file_get_contents(__DIR__.'/../Fixtures/DummyWithUsesWindowsLineEndings.php'), "\r\n"));
89+
90+
$uses = [
91+
'Type' => Type::class,
92+
\DateTimeInterface::class => '\\'.\DateTimeInterface::class,
93+
'DateTime' => '\\'.\DateTimeImmutable::class,
94+
];
95+
96+
$this->assertSame($uses, $this->typeContextFactory->createFromClassName(DummyWithUsesWindowsLineEndings::class)->uses);
97+
98+
$this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionClass(DummyWithUsesWindowsLineEndings::class))->uses);
99+
$this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionProperty(DummyWithUsesWindowsLineEndings::class, 'createdAt'))->uses);
100+
$this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionMethod(DummyWithUsesWindowsLineEndings::class, 'setCreatedAt'))->uses);
101+
$this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionParameter([DummyWithUsesWindowsLineEndings::class, 'setCreatedAt'], 'createdAt'))->uses);
102+
}
103+
85104
public function testCollectTemplates()
86105
{
87106
$this->assertEquals([], $this->typeContextFactory->createFromClassName(Dummy::class)->templates);

src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private function collectUses(\ReflectionClass $reflection): array
116116
return [];
117117
}
118118

119-
if (false === $lines = @file($fileName)) {
119+
if (false === $lines = @file($fileName, \FILE_IGNORE_NEW_LINES)) {
120120
throw new RuntimeException(\sprintf('Unable to read file "%s".', $fileName));
121121
}
122122

@@ -126,7 +126,7 @@ private function collectUses(\ReflectionClass $reflection): array
126126
foreach ($lines as $line) {
127127
if (str_starts_with($line, 'use ')) {
128128
$inUseSection = true;
129-
$use = explode(' as ', substr($line, 4, -2), 2);
129+
$use = explode(' as ', substr($line, 4, -1), 2);
130130

131131
$alias = 1 === \count($use) ? substr($use[0], false !== ($p = strrpos($use[0], '\\')) ? 1 + $p : 0) : $use[1];
132132
$uses[$alias] = $use[0];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy