Skip to content

Commit

Permalink
Skip parsing attributes for PHP < 8 (#5)
Browse files Browse the repository at this point in the history
`ReflectionClass::getAttributes()` isn't available before PHP 8, so skip parsing attributes before that version.

Since the `AnnotationDriver` is also used for annotation-based parsing, we need not only skip the test, but instead skip _attibute_ parsing in the code.

Running a skipped test again revealed some more errors for the minimum requirements specified previously.
  • Loading branch information
mpdude authored Apr 25, 2022
1 parent 160427f commit 7b61693
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

"require": {
"php": "^7.4|8.0.*|8.1.*",
"doctrine/annotations": "^1.12",
"jms/metadata": "^2.6.1",
"symfony/property-access": "^2.2|^3.0|^4.0|^5.0"
"symfony/property-access": "^3.4|^4.0|^5.0"
},

"require-dev": {
"doctrine/common": "^2.2",
"phpunit/phpunit": "^8.0|^9.0",
"phpunit/phpunit": "^8.5.26|^9.0",
"symfony/routing": "^2.2|^3.0|^4.0",
"symfony/yaml": "^3.0|^4.0|^5.0",
"twig/twig": "^2.0|^3.0"
Expand Down
9 changes: 6 additions & 3 deletions src/JMS/ObjectRouting/Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ public function loadMetadataForClass(\ReflectionClass $class): ?ClassMetadata
private function buildAnnotations(\ReflectionClass $class): array
{
$annots = [];
foreach ($class->getAttributes() as $attr) {
if (str_starts_with($attr->getName(), 'JMS\\ObjectRouting\\Annotation\\')) {
$annots[] = $attr->newInstance();

if (PHP_MAJOR_VERSION >= 8) {
foreach ($class->getAttributes() as $attr) {
if (str_starts_with($attr->getName(), 'JMS\\ObjectRouting\\Annotation\\')) {
$annots[] = $attr->newInstance();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@

use Doctrine\Common\Annotations\AnnotationReader;
use JMS\ObjectRouting\Metadata\Driver\AnnotationDriver;
use JMS\Tests\ObjectRouting\Metadata\Driver\Fixture\BlogPost;
use PHPUnit\Framework\TestCase;

class AnnotationDriverTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (PHP_MAJOR_VERSION < 8) {
self::markTestSkipped(AnnotationDriver::class . ' is only supported for PHP ^8.0');
}
}

/** @var AnnotationDriver */
private $driver;

public function testLoad()
{
$metadata = $this->driver->loadMetadataForClass(new \ReflectionClass('JMS\Tests\ObjectRouting\Metadata\Driver\Fixture\BlogPost'));
$metadata = $this->driver->loadMetadataForClass(new \ReflectionClass(BlogPost::class));
$this->assertCount(2, $metadata->routes);

$routes = array(
Expand Down

0 comments on commit 7b61693

Please sign in to comment.