Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace deprecated AnnotationsParser from Nette with BetterReflection
- Loading branch information
Showing
10 changed files
with
121 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Utils\Annotations; | ||
|
||
use Lmc\Steward\Utils\Annotations\ClassParser; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
/** | ||
* @covers \Lmc\Steward\Utils\Annotations\ClassParser | ||
*/ | ||
class ClassParserTest extends TestCase | ||
{ | ||
public function testShouldGetClassNameFromFile(): void | ||
{ | ||
$file = $this->createFileInfo('ClassNoDocBlock.php'); | ||
|
||
$className = ClassParser::readClassNameFromFile($file); | ||
|
||
$this->assertSame('Lmc\Steward\Utils\Fixtures\ClassNoDocBlock', $className); | ||
} | ||
|
||
public function testShouldThrowExceptionForMultipleClassesInOneFile(): void | ||
{ | ||
$file = $this->createFileInfo('MultipleClassesInFile.php'); | ||
|
||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessageRegExp('/^File ".+MultipleClassesInFile.php" contains definition of 2 classes\./'); | ||
ClassParser::readClassNameFromFile($file); | ||
} | ||
|
||
public function testShouldThrowExceptionForNoClassInOneFile(): void | ||
{ | ||
$file = $this->createFileInfo('NoClassInFile.php'); | ||
|
||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessageRegExp('/^No class found in file ".+NoClassInFile.php"/'); | ||
ClassParser::readClassNameFromFile($file); | ||
} | ||
|
||
private function createFileInfo(string $fileName): SplFileInfo | ||
{ | ||
return new SplFileInfo(__DIR__ . '/Fixtures/' . $fileName, 'Fixtures/', 'Fixtures/' . $fileName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Steward\Utils\Fixtures; | ||
|
||
class ClassNoDocBlock | ||
{ | ||
} |
11 changes: 11 additions & 0 deletions
11
src-tests/Utils/Annotations/Fixtures/MultipleClassesInFile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Steward\Utils\Fixtures; | ||
|
||
class ClassFirst | ||
{ | ||
} | ||
|
||
class ClassSecond | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
// In this file is no instantiable class declared | ||
|
||
if (PHP_MAJOR_VERSION === 8) { | ||
// do nothing | ||
} else { | ||
// do nothing as well | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Steward\Utils\Annotations; | ||
|
||
use Roave\BetterReflection\BetterReflection; | ||
use Roave\BetterReflection\Reflector\ClassReflector; | ||
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
class ClassParser | ||
{ | ||
public static function readClassNameFromFile(SplFileInfo $file): string | ||
{ | ||
$reflection = new ClassReflector( | ||
new SingleFileSourceLocator($file->getRealPath(), (new BetterReflection())->astLocator()) | ||
); | ||
|
||
$classesInFile = $reflection->getAllClasses(); | ||
self::assertOneClassInFile($classesInFile, $file); | ||
|
||
return $classesInFile[0]->getName(); | ||
} | ||
|
||
private static function assertOneClassInFile(array $classesInFile, SplFileInfo $file): void | ||
{ | ||
if (count($classesInFile) === 0) { | ||
throw new \RuntimeException(sprintf('No class found in file "%s"', $file->getRelativePathname())); | ||
} | ||
|
||
if (count($classesInFile) > 1) { | ||
throw new \RuntimeException( | ||
sprintf( | ||
'File "%s" contains definition of %d classes. However, each class must be defined in its own' | ||
. ' separate file.', | ||
$file->getRelativePathname(), | ||
count($classesInFile) | ||
) | ||
); | ||
} | ||
} | ||
} |