Skip to content

Commit 44d8690

Browse files
antiftwAntolin Janssen
and
Antolin Janssen
authored
Symfony 7 support continuation (#175)
* 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]>
1 parent d26fba7 commit 44d8690

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+113
-360
lines changed

.github/workflows/static.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
steps:
1010
- uses: actions/checkout@master
1111
- name: PHP-CS-Fixer
12-
uses: docker://jakzal/phpqa:php7.3-alpine
12+
uses: docker://jakzal/phpqa:php8.1-alpine
1313
with:
1414
args: php-cs-fixer fix --diff --dry-run -vvv
1515

@@ -19,6 +19,6 @@ jobs:
1919
steps:
2020
- uses: actions/checkout@master
2121
- name: PHPStan
22-
uses: docker://jakzal/phpqa:php7.3-alpine
22+
uses: docker://jakzal/phpqa:php8.1-alpine
2323
with:
2424
args: phpstan analyze --no-progress

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
}
1010
],
1111
"require": {
12-
"php": "^7.2 || ^8.0",
13-
"nikic/php-parser": "^3.0 || ^4.0",
14-
"symfony/finder": "^3.4 || ^4.4 || ^5.0 || ^6.0",
12+
"php": "^8.1",
13+
"nikic/php-parser": "^4.0 || ^5.0",
14+
"symfony/finder": "^5.4 || ^6.4 || ^7.0",
1515
"twig/twig": "^2.0 || ^3.0",
1616
"doctrine/annotations": "^1.7 || ^2.0"
1717
},
1818
"require-dev": {
19-
"symfony/phpunit-bridge": "^5.0 || ^6.0",
20-
"symfony/translation": "^3.4 || ^4.4 || ^5.0 || ^6.0",
21-
"symfony/validator": "^3.4 || ^4.4 || ^5.0 || ^6.0",
22-
"symfony/twig-bridge": "^3.4 || ^4.4 || ^5.0 || ^6.0",
19+
"symfony/phpunit-bridge": "^5.4 || ^6.4 || ^7.0",
20+
"symfony/translation": "^5.4 || ^6.4 || ^7.0",
21+
"symfony/validator": "^5.4 || ^6.4 || ^7.0",
22+
"symfony/twig-bridge": "^5.4 || ^6.4 || ^7.0",
2323
"knplabs/knp-menu": "^3.1"
2424
},
2525
"autoload": {

src/Extractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class Extractor
2727
/**
2828
* @var FileExtractor[]
2929
*/
30-
private $fileExtractors = [];
30+
private array $fileExtractors = [];
3131

3232
public function extract(Finder $finder): SourceCollection
3333
{

src/FileExtractor/BladeFileExtractor.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
*/
2121
final class BladeFileExtractor implements FileExtractor
2222
{
23-
/**
24-
* {@inheritdoc}
25-
*/
2623
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
2724
{
2825
$realPath = $file->getRealPath();
@@ -64,9 +61,6 @@ public function findTranslations(SplFileInfo $file): array
6461
return $keys;
6562
}
6663

67-
/**
68-
* {@inheritdoc}
69-
*/
7064
public function supportsExtension(string $extension): bool
7165
{
7266
return 'blade.php' === $extension;

src/FileExtractor/PHPFileExtractor.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpParser\NodeTraverser;
1616
use PhpParser\NodeVisitor;
1717
use PhpParser\ParserFactory;
18+
use PhpParser\PhpVersion;
1819
use Symfony\Component\Finder\SplFileInfo;
1920
use Translation\Extractor\Model\SourceCollection;
2021
use Translation\Extractor\Visitor\Visitor;
@@ -27,15 +28,12 @@ final class PHPFileExtractor implements FileExtractor
2728
/**
2829
* @var Visitor[]|NodeVisitor[]
2930
*/
30-
private $visitors = [];
31+
private array $visitors = [];
3132

32-
/**
33-
* {@inheritdoc}
34-
*/
3533
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
3634
{
3735
$path = $file->getRelativePath();
38-
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
36+
$parser = (new ParserFactory())->createForVersion(PhpVersion::fromString('8.1'));
3937
$traverser = new NodeTraverser();
4038
foreach ($this->visitors as $v) {
4139
$v->init($collection, $file);
@@ -50,9 +48,6 @@ public function getSourceLocations(SplFileInfo $file, SourceCollection $collecti
5048
}
5149
}
5250

53-
/**
54-
* {@inheritdoc}
55-
*/
5651
public function supportsExtension(string $extension): bool
5752
{
5853
return \in_array($extension, ['php', 'php5', 'phtml']);

src/FileExtractor/TwigFileExtractor.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,13 @@ final class TwigFileExtractor extends AbstractExtension implements FileExtractor
2727
/**
2828
* @var NodeVisitorInterface[]
2929
*/
30-
private $visitors = [];
31-
private $twig;
30+
private array $visitors = [];
3231

33-
public function __construct(Environment $twig)
32+
public function __construct(private readonly Environment $twig)
3433
{
35-
$this->twig = $twig;
3634
$twig->addExtension($this);
3735
}
3836

39-
/**
40-
* {@inheritdoc}
41-
*/
4237
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection): void
4338
{
4439
foreach ($this->visitors as $v) {
@@ -53,9 +48,6 @@ public function getSourceLocations(SplFileInfo $file, SourceCollection $collecti
5348
$this->twig->parse($stream);
5449
}
5550

56-
/**
57-
* {@inheritdoc}
58-
*/
5951
public function supportsExtension(string $extension): bool
6052
{
6153
return 'twig' === $extension;
@@ -66,17 +58,11 @@ public function addVisitor(NodeVisitorInterface $visitor): void
6658
$this->visitors[] = $visitor;
6759
}
6860

69-
/**
70-
* {@inheritdoc}
71-
*/
7261
public function getNodeVisitors(): array
7362
{
7463
return $this->visitors;
7564
}
7665

77-
/**
78-
* {@inheritdoc}
79-
*/
8066
public function getName(): string
8167
{
8268
return 'php.translation';

src/Model/Error.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818
*/
1919
final class Error
2020
{
21-
private $message;
22-
private $path;
23-
private $line;
24-
25-
public function __construct(string $message, string $path, int $line)
26-
{
27-
$this->message = $message;
28-
$this->path = (string) $path;
29-
$this->line = $line;
21+
public function __construct(
22+
private readonly string $message,
23+
private readonly string $path,
24+
private readonly int $line
25+
) {
3026
}
3127

3228
public function getMessage(): string

src/Model/SourceCollection.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,18 @@ final class SourceCollection implements \Countable, \IteratorAggregate
1919
/**
2020
* @var SourceLocation[]
2121
*/
22-
private $sourceLocations = [];
22+
private array $sourceLocations = [];
2323

2424
/**
2525
* @var Error[]
2626
*/
27-
private $errors = [];
27+
private array $errors = [];
2828

29-
/**
30-
* {@inheritdoc}
31-
*/
3229
public function getIterator(): \Traversable
3330
{
3431
return new \ArrayIterator($this->sourceLocations);
3532
}
3633

37-
/**
38-
* {@inheritdoc}
39-
*/
4034
public function count(): int
4135
{
4236
return \count($this->sourceLocations);

src/Model/SourceLocation.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,12 @@
1616
*/
1717
final class SourceLocation
1818
{
19-
/**
20-
* Translation key.
21-
*/
22-
private $message;
23-
private $path;
24-
private $line;
25-
private $context;
26-
27-
public function __construct(string $message, string $path, int $line, array $context = [])
28-
{
29-
$this->message = $message;
30-
$this->path = (string) $path;
31-
$this->line = $line;
32-
$this->context = $context;
19+
public function __construct(
20+
private readonly string $message, /** Translation key. */
21+
private readonly string $path,
22+
private readonly int $line,
23+
private readonly array $context = []
24+
) {
3325
}
3426

3527
/**

src/Twig/TranslationExtension.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
final class TranslationExtension extends AbstractExtension
1818
{
19-
/**
20-
* {@inheritdoc}
21-
*/
2219
public function getFilters(): array
2320
{
2421
return [
@@ -31,9 +28,6 @@ public function runDescFilter($v)
3128
return $v;
3229
}
3330

34-
/**
35-
* {@inheritdoc}
36-
*/
3731
public function getName(): string
3832
{
3933
return 'php-translation';

src/Visitor/BaseVisitor.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,11 @@
2727
*/
2828
abstract class BaseVisitor implements Visitor
2929
{
30-
protected $collection;
31-
protected $file;
30+
private ?DocParser $docParser = null;
3231

33-
/**
34-
* @var DocParser
35-
*/
36-
private $docParser;
32+
protected ?SourceCollection $collection = null;
33+
protected SplFileInfo $file;
3734

38-
/**
39-
* {@inheritdoc}
40-
*/
4135
public function init(SourceCollection $collection, SplFileInfo $file): void
4236
{
4337
$this->collection = $collection;
@@ -71,7 +65,7 @@ protected function addError(Node $node, string $errorMessage): void
7165
$this->collection->addError(new Error($errorMessage, $file, $line));
7266
}
7367

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

83-
protected function getLocation(string $text, int $line, Node $node = null, array $context = []): ?SourceLocation
77+
protected function getLocation(string $text, int $line, ?Node $node = null, array $context = []): ?SourceLocation
8478
{
8579
$file = $this->getAbsoluteFilePath();
8680
if (null !== $node && null !== $docComment = $node->getDocComment()) {

src/Visitor/Php/Knp/Menu/AbstractKnpMenuVisitor.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,14 @@
2121
*/
2222
abstract class AbstractKnpMenuVisitor extends BasePHPVisitor implements NodeVisitor
2323
{
24-
/**
25-
* @var bool
26-
*/
27-
private $isKnpMenuBuildingMethod = false;
24+
private bool $isKnpMenuBuildingMethod = false;
2825

29-
/**
30-
* @var string|bool
31-
*/
32-
private $domain;
26+
private string|bool|null $domain = null;
3327

3428
/**
3529
* @var SourceLocation[]
3630
*/
37-
private $sourceLocations = [];
31+
private array $sourceLocations = [];
3832

3933
public function beforeTraverse(array $nodes): ?Node
4034
{
@@ -126,7 +120,6 @@ public function afterTraverse(array $nodes): ?Node
126120
return null;
127121
}
128122

129-
/** @var SourceLocation $location */
130123
foreach ($this->sourceLocations as $location) {
131124
if (null !== $this->domain) {
132125
$context = $location->getContext();

src/Visitor/Php/SourceLocationContainerVisitor.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,29 @@
2323
*/
2424
final class SourceLocationContainerVisitor extends BasePHPVisitor implements NodeVisitor
2525
{
26-
/**
27-
* @var string
28-
*/
29-
private $namespace = '';
30-
31-
/**
32-
* @var array
33-
*/
34-
private $useStatements = [];
35-
36-
/**
37-
* {@inheritdoc}
38-
*/
26+
private string $namespace = '';
27+
private array $useStatements = [];
28+
3929
public function beforeTraverse(array $nodes): ?Node
4030
{
4131
return null;
4232
}
4333

44-
/**
45-
* {@inheritdoc}
46-
*/
4734
public function enterNode(Node $node): ?Node
4835
{
4936
if ($node instanceof Node\Stmt\Namespace_) {
5037
if (isset($node->name)) {
5138
// Save namespace of this class for later.
52-
$this->namespace = implode('\\', $node->name->parts);
39+
$this->namespace = implode('\\', $node->name->getParts());
5340
}
5441
$this->useStatements = [];
5542

5643
return null;
5744
}
5845

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

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

7057
$isContainer = false;
7158
foreach ($node->implements as $interface) {
72-
$name = implode('\\', $interface->parts);
59+
$name = implode('\\', $interface->getParts());
7360
if (isset($this->useStatements[$name])) {
7461
$name = $this->useStatements[$name];
7562
}
@@ -98,17 +85,11 @@ public function enterNode(Node $node): ?Node
9885
return null;
9986
}
10087

101-
/**
102-
* {@inheritdoc}
103-
*/
10488
public function leaveNode(Node $node): ?Node
10589
{
10690
return null;
10791
}
10892

109-
/**
110-
* {@inheritdoc}
111-
*/
11293
public function afterTraverse(array $nodes): ?Node
11394
{
11495
return null;

0 commit comments

Comments
 (0)