Skip to content

Commit

Permalink
Symfony 7 support continuation (#175)
Browse files Browse the repository at this point in the history
* initial commit migrating annotations to attributes

* minor tweak in Translate attribute, so you dont have to pass a default translation domain

* minor refactor entities so they use property promotion

* remove some comments that werent supposed to be there

* removed dependency on lower symfony versions + switched to deprecation annotations + bugfixes to pass some tests

* replaced AnnotationLoader with AttributeLoader in some tests

* changed php requirement, added ^7.2 back since the STAN and CSFixer tests run on PHP7.3

* bumped to 8.1

* reverted custom attribute changes, except the SymfonyValidation AnnotationReader which was deprecated in 6.4

* some more use statements that apparently did not influence the tests

* all but one PHPSTAN tests passing

* fixed last phpstan test?

* weird issue

* fixed errors that originated in bumping nikic/php-parser to 5.0

* removed support for 3.0 nikic/php-parser

* removed superfluous test

---------

Co-authored-by: Antolin Janssen <[email protected]>
  • Loading branch information
antiftw and Antolin Janssen authored Jun 11, 2024
1 parent d26fba7 commit 44d8690
Show file tree
Hide file tree
Showing 41 changed files with 113 additions and 360 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: PHP-CS-Fixer
uses: docker://jakzal/phpqa:php7.3-alpine
uses: docker://jakzal/phpqa:php8.1-alpine
with:
args: php-cs-fixer fix --diff --dry-run -vvv

Expand All @@ -19,6 +19,6 @@ jobs:
steps:
- uses: actions/checkout@master
- name: PHPStan
uses: docker://jakzal/phpqa:php7.3-alpine
uses: docker://jakzal/phpqa:php8.1-alpine
with:
args: phpstan analyze --no-progress
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
}
],
"require": {
"php": "^7.2 || ^8.0",
"nikic/php-parser": "^3.0 || ^4.0",
"symfony/finder": "^3.4 || ^4.4 || ^5.0 || ^6.0",
"php": "^8.1",
"nikic/php-parser": "^4.0 || ^5.0",
"symfony/finder": "^5.4 || ^6.4 || ^7.0",
"twig/twig": "^2.0 || ^3.0",
"doctrine/annotations": "^1.7 || ^2.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0 || ^6.0",
"symfony/translation": "^3.4 || ^4.4 || ^5.0 || ^6.0",
"symfony/validator": "^3.4 || ^4.4 || ^5.0 || ^6.0",
"symfony/twig-bridge": "^3.4 || ^4.4 || ^5.0 || ^6.0",
"symfony/phpunit-bridge": "^5.4 || ^6.4 || ^7.0",
"symfony/translation": "^5.4 || ^6.4 || ^7.0",
"symfony/validator": "^5.4 || ^6.4 || ^7.0",
"symfony/twig-bridge": "^5.4 || ^6.4 || ^7.0",
"knplabs/knp-menu": "^3.1"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class Extractor
/**
* @var FileExtractor[]
*/
private $fileExtractors = [];
private array $fileExtractors = [];

public function extract(Finder $finder): SourceCollection
{
Expand Down
6 changes: 0 additions & 6 deletions src/FileExtractor/BladeFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
*/
final class BladeFileExtractor implements FileExtractor
{
/**
* {@inheritdoc}
*/
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
{
$realPath = $file->getRealPath();
Expand Down Expand Up @@ -64,9 +61,6 @@ public function findTranslations(SplFileInfo $file): array
return $keys;
}

/**
* {@inheritdoc}
*/
public function supportsExtension(string $extension): bool
{
return 'blade.php' === $extension;
Expand Down
11 changes: 3 additions & 8 deletions src/FileExtractor/PHPFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use Symfony\Component\Finder\SplFileInfo;
use Translation\Extractor\Model\SourceCollection;
use Translation\Extractor\Visitor\Visitor;
Expand All @@ -27,15 +28,12 @@ final class PHPFileExtractor implements FileExtractor
/**
* @var Visitor[]|NodeVisitor[]
*/
private $visitors = [];
private array $visitors = [];

/**
* {@inheritdoc}
*/
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
{
$path = $file->getRelativePath();
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromString('8.1'));
$traverser = new NodeTraverser();
foreach ($this->visitors as $v) {
$v->init($collection, $file);
Expand All @@ -50,9 +48,6 @@ public function getSourceLocations(SplFileInfo $file, SourceCollection $collecti
}
}

/**
* {@inheritdoc}
*/
public function supportsExtension(string $extension): bool
{
return \in_array($extension, ['php', 'php5', 'phtml']);
Expand Down
18 changes: 2 additions & 16 deletions src/FileExtractor/TwigFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ final class TwigFileExtractor extends AbstractExtension implements FileExtractor
/**
* @var NodeVisitorInterface[]
*/
private $visitors = [];
private $twig;
private array $visitors = [];

public function __construct(Environment $twig)
public function __construct(private readonly Environment $twig)
{
$this->twig = $twig;
$twig->addExtension($this);
}

/**
* {@inheritdoc}
*/
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
{
foreach ($this->visitors as $v) {
Expand All @@ -53,9 +48,6 @@ public function getSourceLocations(SplFileInfo $file, SourceCollection $collecti
$this->twig->parse($stream);
}

/**
* {@inheritdoc}
*/
public function supportsExtension(string $extension): bool
{
return 'twig' === $extension;
Expand All @@ -66,17 +58,11 @@ public function addVisitor(NodeVisitorInterface $visitor): void
$this->visitors[] = $visitor;
}

/**
* {@inheritdoc}
*/
public function getNodeVisitors(): array
{
return $this->visitors;
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'php.translation';
Expand Down
14 changes: 5 additions & 9 deletions src/Model/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
*/
final class Error
{
private $message;
private $path;
private $line;

public function __construct(string $message, string $path, int $line)
{
$this->message = $message;
$this->path = (string) $path;
$this->line = $line;
public function __construct(
private readonly string $message,
private readonly string $path,
private readonly int $line
) {
}

public function getMessage(): string
Expand Down
10 changes: 2 additions & 8 deletions src/Model/SourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,18 @@ final class SourceCollection implements \Countable, \IteratorAggregate
/**
* @var SourceLocation[]
*/
private $sourceLocations = [];
private array $sourceLocations = [];

/**
* @var Error[]
*/
private $errors = [];
private array $errors = [];

/**
* {@inheritdoc}
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->sourceLocations);
}

/**
* {@inheritdoc}
*/
public function count(): int
{
return \count($this->sourceLocations);
Expand Down
20 changes: 6 additions & 14 deletions src/Model/SourceLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,12 @@
*/
final class SourceLocation
{
/**
* Translation key.
*/
private $message;
private $path;
private $line;
private $context;

public function __construct(string $message, string $path, int $line, array $context = [])
{
$this->message = $message;
$this->path = (string) $path;
$this->line = $line;
$this->context = $context;
public function __construct(
private readonly string $message, /** Translation key. */
private readonly string $path,
private readonly int $line,
private readonly array $context = []
) {
}

/**
Expand Down
6 changes: 0 additions & 6 deletions src/Twig/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

final class TranslationExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
Expand All @@ -31,9 +28,6 @@ public function runDescFilter($v)
return $v;
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'php-translation';
Expand Down
16 changes: 5 additions & 11 deletions src/Visitor/BaseVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@
*/
abstract class BaseVisitor implements Visitor
{
protected $collection;
protected $file;
private ?DocParser $docParser = null;

/**
* @var DocParser
*/
private $docParser;
protected ?SourceCollection $collection = null;
protected SplFileInfo $file;

/**
* {@inheritdoc}
*/
public function init(SourceCollection $collection, SplFileInfo $file): void
{
$this->collection = $collection;
Expand Down Expand Up @@ -71,7 +65,7 @@ protected function addError(Node $node, string $errorMessage): void
$this->collection->addError(new Error($errorMessage, $file, $line));
}

protected function addLocation(string $text, int $line, Node $node = null, array $context = []): void
protected function addLocation(string $text, int $line, ?Node $node = null, array $context = []): void
{
if (null === $location = $this->getLocation($text, $line, $node, $context)) {
return;
Expand All @@ -80,7 +74,7 @@ protected function addLocation(string $text, int $line, Node $node = null, array
$this->collection->addLocation($location);
}

protected function getLocation(string $text, int $line, Node $node = null, array $context = []): ?SourceLocation
protected function getLocation(string $text, int $line, ?Node $node = null, array $context = []): ?SourceLocation
{
$file = $this->getAbsoluteFilePath();
if (null !== $node && null !== $docComment = $node->getDocComment()) {
Expand Down
13 changes: 3 additions & 10 deletions src/Visitor/Php/Knp/Menu/AbstractKnpMenuVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,14 @@
*/
abstract class AbstractKnpMenuVisitor extends BasePHPVisitor implements NodeVisitor
{
/**
* @var bool
*/
private $isKnpMenuBuildingMethod = false;
private bool $isKnpMenuBuildingMethod = false;

/**
* @var string|bool
*/
private $domain;
private string|bool|null $domain = null;

/**
* @var SourceLocation[]
*/
private $sourceLocations = [];
private array $sourceLocations = [];

public function beforeTraverse(array $nodes): ?Node
{
Expand Down Expand Up @@ -126,7 +120,6 @@ public function afterTraverse(array $nodes): ?Node
return null;
}

/** @var SourceLocation $location */
foreach ($this->sourceLocations as $location) {
if (null !== $this->domain) {
$context = $location->getContext();
Expand Down
33 changes: 7 additions & 26 deletions src/Visitor/Php/SourceLocationContainerVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,29 @@
*/
final class SourceLocationContainerVisitor extends BasePHPVisitor implements NodeVisitor
{
/**
* @var string
*/
private $namespace = '';

/**
* @var array
*/
private $useStatements = [];

/**
* {@inheritdoc}
*/
private string $namespace = '';
private array $useStatements = [];

public function beforeTraverse(array $nodes): ?Node
{
return null;
}

/**
* {@inheritdoc}
*/
public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Stmt\Namespace_) {
if (isset($node->name)) {
// Save namespace of this class for later.
$this->namespace = implode('\\', $node->name->parts);
$this->namespace = implode('\\', $node->name->getParts());
}
$this->useStatements = [];

return null;
}

if ($node instanceof Node\Stmt\UseUse) {
$key = isset($node->alias) ? $node->alias : $node->name->parts[\count($node->name->parts) - 1];
$this->useStatements[(string) $key] = implode('\\', $node->name->parts);
$key = $node->alias ?? $node->name->getParts()[\count($node->name->getParts()) - 1];
$this->useStatements[(string) $key] = implode('\\', $node->name->getParts());

return null;
}
Expand All @@ -69,7 +56,7 @@ public function enterNode(Node $node): ?Node

$isContainer = false;
foreach ($node->implements as $interface) {
$name = implode('\\', $interface->parts);
$name = implode('\\', $interface->getParts());
if (isset($this->useStatements[$name])) {
$name = $this->useStatements[$name];
}
Expand Down Expand Up @@ -98,17 +85,11 @@ public function enterNode(Node $node): ?Node
return null;
}

/**
* {@inheritdoc}
*/
public function leaveNode(Node $node): ?Node
{
return null;
}

/**
* {@inheritdoc}
*/
public function afterTraverse(array $nodes): ?Node
{
return null;
Expand Down
Loading

0 comments on commit 44d8690

Please sign in to comment.