Skip to content

Commit 399c0bd

Browse files
authored
Static fixes, upgrade tests to PHP 7.4 and PHP 8.0 syntax (#654)
* static fixes * add check for PHP comented code * enable autoimports in Rector * apply PHP 7.4 and PHP 8.0 on src code, except traits * fix name
1 parent 47a4fa9 commit 399c0bd

File tree

61 files changed

+267
-365
lines changed

Some content is hidden

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

61 files changed

+267
-365
lines changed

.github/workflows/code_checks.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ jobs:
2323
name: 'Rector'
2424
run: vendor/bin/rector process --dry-run --ansi
2525

26+
-
27+
name: 'Commented PHP Code'
28+
run: vendor/bin/easy-ci check-commented-code src tests --line-limit 3
29+
2630
name: ${{ matrix.actions.name }}
2731
runs-on: ubuntu-latest
2832

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"symplify/phpstan-rules": "^9.4",
4747
"symplify/config-transformer": "^9.4",
4848
"symplify/amnesia": "^9.4",
49-
"phpstan/extension-installer": "^1.1"
49+
"phpstan/extension-installer": "^1.1",
50+
"symplify/easy-ci": "^9.4"
5051
},
5152
"autoload": {
5253
"psr-4": {

phpstan.neon

+12-1
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,15 @@ parameters:
9999
# known retype
100100
- '#Cannot cast array\|bool\|float\|int\|string\|null to string#'
101101

102-
- '#Method Knp\\DoctrineBehaviors\\Tests\\Fixtures\\(.*?) return type with generic interface Doctrine\\Common\\Collections\\Collection does not specify its types\: TKey, T#'
102+
- '#Method Knp\\DoctrineBehaviors\\Tests\\Fixtures\\(.*?) return type with generic interface Doctrine\\Common\\Collections\\Collection does not specify its types\: TKey, T#'
103+
104+
-
105+
message: '#Use required typed property over of nullable property#'
106+
path: tests/Provider/TestUserProvider.php
107+
108+
- '#"Doctrine\\ORM\\EntityManagerInterface" dependency is allowed only in "\*Repository", "Doctrine\\Bundle\\DoctrineBundle\\EventSubscriber\\EventSubscriberInterface" types#'
109+
110+
# @todo fix in symplify, common use case for entities
111+
-
112+
message: '#Use required typed property over of nullable property#'
113+
path: '*/Entity/*'

rector.php

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
$parameters = $containerConfigurator->parameters();
1313

1414
$parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/utils']);
15+
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
1516

1617
$parameters->set(Option::SKIP, [
1718
RenamePropertyToMatchTypeRector::class => [__DIR__ . '/tests/ORM/'],
@@ -26,4 +27,8 @@
2627
$containerConfigurator->import(SetList::PHP_71);
2728
$containerConfigurator->import(SetList::PHP_72);
2829
$containerConfigurator->import(SetList::PHP_73);
30+
31+
// for tests only
32+
// $containerConfigurator->import(SetList::PHP_74);
33+
// $containerConfigurator->import(SetList::PHP_80);
2934
};

src/Contract/Entity/TreeNodeInterface.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public function setParentMaterializedPath(string $path): void;
3737

3838
public function getParentNode(): ?self;
3939

40+
public function setParentNode(self $treeNode): void;
41+
4042
/**
4143
* @param string $path the materialized path, eg: the the materialized path to its parent
4244
*/
@@ -50,9 +52,9 @@ public function setMaterializedPath(string $path): void;
5052
* - Remove this node from the children of the old parent
5153
* - Modify the materialized path of this node and all its children, recursively
5254
*/
53-
public function setChildNodeOf(?self $node = null): void;
55+
public function setChildNodeOf(?self $treeNode = null): void;
5456

55-
public function addChildNode(self $node): void;
57+
public function addChildNode(self $treeNode): void;
5658

5759
/**
5860
* @return Collection<TreeNodeInterface>
@@ -65,12 +67,14 @@ public function isRootNode(): bool;
6567

6668
public function getRootNode(): self;
6769

68-
public function isChildNodeOf(self $node): bool;
70+
public function isChildNodeOf(self $treeNode): bool;
6971

7072
public function getNodeLevel(): int;
7173

7274
/**
7375
* Builds a hierarchical tree from a flat collection of NodeInterface elements
76+
*
77+
* @param self[] $treeNodes
7478
*/
75-
public function buildTree(array $nodes): void;
79+
public function buildTree(array $treeNodes): void;
7680
}

src/EventSubscriber/BlameableEventSubscriber.php

+3-21
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,11 @@ final class BlameableEventSubscriber implements EventSubscriberInterface
3131
*/
3232
private const CREATED_BY = 'createdBy';
3333

34-
/**
35-
* @var string|null
36-
*/
37-
private $blameableUserEntity;
38-
39-
/**
40-
* @var UserProviderInterface
41-
*/
42-
private $userProvider;
43-
44-
/**
45-
* @var EntityManagerInterface
46-
*/
47-
private $entityManager;
48-
4934
public function __construct(
50-
UserProviderInterface $userProvider,
51-
EntityManagerInterface $entityManager,
52-
?string $blameableUserEntity = null
35+
private UserProviderInterface $userProvider,
36+
private EntityManagerInterface $entityManager,
37+
private ?string $blameableUserEntity = null
5338
) {
54-
$this->userProvider = $userProvider;
55-
$this->entityManager = $entityManager;
56-
$this->blameableUserEntity = $blameableUserEntity;
5739
}
5840

5941
/**

src/EventSubscriber/LoggableEventSubscriber.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313

1414
final class LoggableEventSubscriber implements EventSubscriberInterface
1515
{
16-
/**
17-
* @var LoggerInterface
18-
*/
19-
private $logger;
20-
21-
public function __construct(LoggerInterface $logger)
22-
{
23-
$this->logger = $logger;
16+
public function __construct(
17+
private LoggerInterface $logger
18+
) {
2419
}
2520

2621
public function postPersist(LifecycleEventArgs $lifecycleEventArgs): void
@@ -72,7 +67,7 @@ private function logChangeSet(LifecycleEventArgs $lifecycleEventArgs): void
7267
$unitOfWork = $entityManager->getUnitOfWork();
7368
$entity = $lifecycleEventArgs->getEntity();
7469

75-
$entityClass = get_class($entity);
70+
$entityClass = $entity::class;
7671
$classMetadata = $entityManager->getClassMetadata($entityClass);
7772

7873
/** @var LoggableInterface $entity */

src/EventSubscriber/SluggableEventSubscriber.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,10 @@ final class SluggableEventSubscriber implements EventSubscriberInterface
2020
*/
2121
private const SLUG = 'slug';
2222

23-
/**
24-
* @var DefaultSluggableRepository
25-
*/
26-
private $defaultSluggableRepository;
27-
28-
/**
29-
* @var EntityManagerInterface
30-
*/
31-
private $entityManager;
32-
3323
public function __construct(
34-
EntityManagerInterface $entityManager,
35-
DefaultSluggableRepository $defaultSluggableRepository
24+
private EntityManagerInterface $entityManager,
25+
private DefaultSluggableRepository $defaultSluggableRepository
3626
) {
37-
$this->defaultSluggableRepository = $defaultSluggableRepository;
38-
$this->entityManager = $entityManager;
3927
}
4028

4129
public function loadClassMetadata(LoadClassMetadataEventArgs $loadClassMetadataEventArgs): void

src/EventSubscriber/TimestampableEventSubscriber.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111

1212
final class TimestampableEventSubscriber implements EventSubscriberInterface
1313
{
14-
/**
15-
* @var string
16-
*/
17-
private $dateFieldType;
18-
19-
public function __construct(string $timestampableDateFieldType)
20-
{
21-
$this->dateFieldType = $timestampableDateFieldType;
14+
public function __construct(
15+
private string $timestampableDateFieldType
16+
) {
2217
}
2318

2419
public function loadClassMetadata(LoadClassMetadataEventArgs $loadClassMetadataEventArgs): void
@@ -44,7 +39,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $loadClassMetadataE
4439
if (! $classMetadata->hasField($field)) {
4540
$classMetadata->mapField([
4641
'fieldName' => $field,
47-
'type' => $this->dateFieldType,
42+
'type' => $this->timestampableDateFieldType,
4843
'nullable' => true,
4944
]);
5045
}

src/EventSubscriber/TranslatableEventSubscriber.php

+3-15
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,15 @@ final class TranslatableEventSubscriber implements EventSubscriberInterface
2020
*/
2121
public const LOCALE = 'locale';
2222

23-
/**
24-
* @var int
25-
*/
26-
private $translatableFetchMode;
23+
private int $translatableFetchMode;
2724

28-
/**
29-
* @var int
30-
*/
31-
private $translationFetchMode;
32-
33-
/**
34-
* @var LocaleProviderInterface
35-
*/
36-
private $localeProvider;
25+
private int $translationFetchMode;
3726

3827
public function __construct(
39-
LocaleProviderInterface $localeProvider,
28+
private LocaleProviderInterface $localeProvider,
4029
string $translatableFetchMode,
4130
string $translationFetchMode
4231
) {
43-
$this->localeProvider = $localeProvider;
4432
$this->translatableFetchMode = $this->convertFetchString($translatableFetchMode);
4533
$this->translationFetchMode = $this->convertFetchString($translationFetchMode);
4634
}

src/Model/Loggable/LoggableTrait.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ public function getUpdateLogMessage(array $changeSets = []): string
3434

3535
public function getCreateLogMessage(): string
3636
{
37-
return sprintf('%s #%s created', self::class, $this->getId());
37+
return sprintf('%s #%d created', self::class, $this->getId());
3838
}
3939

4040
public function getRemoveLogMessage(): string
4141
{
42-
return sprintf('%s #%s removed', self::class, $this->getId());
42+
return sprintf('%s #%d removed', self::class, $this->getId());
4343
}
4444

4545
private function createChangeSetMessage(string $property, array $changeSet): string
4646
{
4747
return sprintf(
48-
'%s #%s : property "%s" changed from "%s" to "%s"',
48+
'%s #%d : property "%s" changed from "%s" to "%s"',
4949
self::class,
5050
$this->getId(),
5151
$property,
52-
is_array($changeSet[0]) ? 'an array' : $changeSet[0],
53-
is_array($changeSet[1]) ? 'an array' : $changeSet[1]
52+
is_array($changeSet[0]) ? 'an array' : (string) $changeSet[0],
53+
is_array($changeSet[1]) ? 'an array' : (string) $changeSet[1]
5454
);
5555
}
5656
}

src/Model/SoftDeletable/SoftDeletableMethodsTrait.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ public function isDeleted(): bool
3333
return false;
3434
}
3535

36-
public function willBeDeleted(?DateTimeInterface $dateTime = null): bool
36+
public function willBeDeleted(?DateTimeInterface $deletedAt = null): bool
3737
{
3838
if ($this->deletedAt === null) {
3939
return false;
4040
}
41-
if ($dateTime === null) {
41+
if ($deletedAt === null) {
4242
return true;
4343
}
4444

45-
return $this->deletedAt <= $dateTime;
45+
return $this->deletedAt <= $deletedAt;
4646
}
4747

4848
public function getDeletedAt(): ?DateTimeInterface

src/Model/Translatable/TranslatableMethodsTrait.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ protected function doTranslate(?string $locale = null, bool $fallbackToDefault =
135135
$locale = $this->getCurrentLocale();
136136
}
137137

138-
$translation = $this->findTranslationByLocale($locale);
139-
if ($translation && ! $translation->isEmpty()) {
140-
return $translation;
138+
$foundTranslation = $this->findTranslationByLocale($locale);
139+
if ($foundTranslation && ! $foundTranslation->isEmpty()) {
140+
return $foundTranslation;
141141
}
142142

143143
if ($fallbackToDefault) {
@@ -147,8 +147,8 @@ protected function doTranslate(?string $locale = null, bool $fallbackToDefault =
147147
}
148148
}
149149

150-
if ($translation) {
151-
return $translation;
150+
if ($foundTranslation) {
151+
return $foundTranslation;
152152
}
153153

154154
$class = static::getTranslationEntityClass();

src/Model/Tree/TreeNodeMethodsTrait.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,21 @@ public function getRootNode(): TreeNodeInterface
163163
return $parent;
164164
}
165165

166-
public function buildTree(array $results): void
166+
/**
167+
* @param TreeNodeInterface[] $treeNodes
168+
*/
169+
public function buildTree(array $treeNodes): void
167170
{
168171
$this->getChildNodes()
169172
->clear();
170-
foreach ($results as $result) {
171-
if ($result->getMaterializedPath() === $this->getRealMaterializedPath()) {
172-
$result->setParentNode($this);
173-
$result->buildTree($results);
173+
174+
foreach ($treeNodes as $treeNode) {
175+
if ($treeNode->getMaterializedPath() !== $this->getRealMaterializedPath()) {
176+
continue;
174177
}
178+
179+
$treeNode->setParentNode($this);
180+
$treeNode->buildTree($treeNodes);
175181
}
176182
}
177183

src/Provider/LocaleProvider.php

+4-23
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,15 @@
1010
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
1111
use Symfony\Component\HttpFoundation\Request;
1212
use Symfony\Component\HttpFoundation\RequestStack;
13-
use Symfony\Contracts\Translation\LocaleAwareInterface;
1413
use Symfony\Contracts\Translation\TranslatorInterface;
1514

1615
final class LocaleProvider implements LocaleProviderInterface
1716
{
18-
/**
19-
* @var TranslatorInterface&LocaleAwareInterface|null
20-
*/
21-
private $translator;
22-
23-
/**
24-
* @var ParameterBagInterface
25-
*/
26-
private $parameterBag;
27-
28-
/**
29-
* @var RequestStack
30-
*/
31-
private $requestStack;
32-
3317
public function __construct(
34-
RequestStack $requestStack,
35-
ParameterBagInterface $parameterBag,
36-
?TranslatorInterface $translator
18+
private RequestStack $requestStack,
19+
private ParameterBagInterface $parameterBag,
20+
private ?TranslatorInterface $translator
3721
) {
38-
$this->requestStack = $requestStack;
39-
$this->translator = $translator;
40-
$this->parameterBag = $parameterBag;
4122
}
4223

4324
public function provideCurrentLocale(): ?string
@@ -72,7 +53,7 @@ public function provideFallbackLocale(): ?string
7253
}
7354

7455
return (string) $this->parameterBag->get('kernel.default_locale');
75-
} catch (ParameterNotFoundException | InvalidArgumentException $parameterNotFoundException) {
56+
} catch (ParameterNotFoundException | InvalidArgumentException) {
7657
return null;
7758
}
7859
}

0 commit comments

Comments
 (0)