Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TypeInfo] use an EOL-agnostic approach to parse class uses #60909

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

Open
wants to merge 1 commit into
base: 7.2
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Symfony/Component/TypeInfo/.gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/Tests export-ignore
/Tests/Fixtures/DummyWithUsesWindowsLineEndings.php text eol=crlf
/phpunit.xml.dist export-ignore
/.git* export-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Symfony\Component\TypeInfo\Tests\Fixtures;

use Symfony\Component\TypeInfo\Type;
use \DateTimeInterface;
use \DateTimeImmutable as DateTime;

final class DummyWithUsesWindowsLineEndings
{
private DateTimeInterface $createdAt;

public function setCreatedAt(DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}

public function getType(): Type
{
throw new \LogicException('Should not be called.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,6 +83,24 @@ public function testCollectUses()
$this->assertEquals($uses, $this->typeContextFactory->createFromReflection(new \ReflectionParameter([DummyWithUses::class, 'setCreatedAt'], 'createdAt'))->uses);
}

public function testCollectUsesWindowsLineEndings()
{
Copy link
Member

Choose a reason for hiding this comment

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

I suggest adding an assertion that the DummyWithUsesWindowsLineEndings.php file contains \r\n, to avoid having someone changing the line endings (by mistake or intentionally) and making this test useless.
This could happen for instance for contributors on Windows using git's newline conversion features (maybe we could even avoid that specific case with a .gitattributes telling git we want Windows line endings in this specific file)

Copy link
Member Author

Choose a reason for hiding this comment

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

I added both the assertion inside the test as well as entries to the relevant .gitattributes files

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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];
Expand Down
Loading