Skip to content

Commit 1597f53

Browse files
authored
Resolve minor code inspections (Case 183951) (#77)
- Fix grammar - Split workflows to reduce indentation/complexity - Improve tests: Add types, remove unnecessary expects-any constraint, use class constant
1 parent dc89a0c commit 1597f53

8 files changed

Lines changed: 29 additions & 35 deletions

File tree

src/Doctrine/PersistentTranslatable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ final class PersistentTranslatable implements TranslatableInterface
6565
* @param class-string $class The class of the entity containing this PersistentTranslatable instance
6666
* @param object $entity The entity containing this PersistentTranslatable instance
6767
* @param string $primaryLocale The locale for which the translated value will be persisted in the "main" entity
68-
* @param DefaultLocaleProvider $defaultLocaleProvider DefaultLocaleProvider that provides the locale to use when no explicit locale is passed to e. g. translate()
68+
* @param DefaultLocaleProvider $defaultLocaleProvider DefaultLocaleProvider that provides the locale to use when no explicit locale is passed to e.g. translate()
6969
* @param ReflectionProperty $translationProperty ReflectionProperty pointing to the field in the translations class that holds the translated value to use
7070
* @param ReflectionProperty $translationCollection ReflectionProperty pointing to the collection in the main class that holds translation instances
7171
* @param ReflectionClass $translationClass ReflectionClass for the class holding translated values

src/Doctrine/PolyglotListener.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ private function loadTranslationMetadataForClass($className, EntityManager $em):
145145
$this->translatedClasses[$className] = null;
146146

147147
return null;
148-
} else {
149-
$wakeup = TranslatableClassMetadata::wakeup($data, $this->reflectionService);
150-
$wakeup->setLogger($this->logger);
151-
$this->translatedClasses[$className] = $wakeup;
152-
153-
return $wakeup;
154148
}
149+
150+
$wakeup = TranslatableClassMetadata::wakeup($data, $this->reflectionService);
151+
$wakeup->setLogger($this->logger);
152+
$this->translatedClasses[$className] = $wakeup;
153+
154+
return $wakeup;
155155
}
156156

157157
// Load/parse

src/EventListener/LocaleListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Webfactory\Bundle\PolyglotBundle\Locale\DefaultLocaleProvider;
1616

1717
/**
18-
* This listeners sets the current locale for the TranslatableListener.
18+
* This listener sets the current locale for the TranslatableListener.
1919
* Taken from Christophe COEVOET's Doctrine Extensions Bundle.
2020
*
2121
* @author Christophe COEVOET

tests/Doctrine/PersistentTranslatableTest.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Webfactory\Bundle\PolyglotBundle\Tests\Doctrine;
44

5+
use Doctrine\Common\Collections\ArrayCollection;
56
use Doctrine\ORM\UnitOfWork;
67
use PHPUnit\Framework\TestCase;
78
use Psr\Log\LoggerInterface;
@@ -17,7 +18,7 @@
1718

1819
class PersistentTranslatableTest extends TestCase
1920
{
20-
public function testToStringReturnsTranslatedMessage()
21+
public function testToStringReturnsTranslatedMessage(): void
2122
{
2223
$entity = new TestEntity('foo');
2324
$proxy = $this->createProxy($entity);
@@ -31,7 +32,7 @@ public function testToStringReturnsTranslatedMessage()
3132
self::assertEquals('bar', $translation);
3233
}
3334

34-
public function testToStringReturnsStringIfExceptionOccurredAndNoLoggerIsAvailable()
35+
public function testToStringReturnsStringIfExceptionOccurredAndNoLoggerIsAvailable(): void
3536
{
3637
$entity = new TestEntity('foo');
3738
$proxy = $this->createProxy($entity);
@@ -41,7 +42,7 @@ public function testToStringReturnsStringIfExceptionOccurredAndNoLoggerIsAvailab
4142
self::assertIsString($translation);
4243
}
4344

44-
public function testToStringReturnsStringIfExceptionOccurredAndLoggerIsAvailable()
45+
public function testToStringReturnsStringIfExceptionOccurredAndLoggerIsAvailable(): void
4546
{
4647
$entity = new TestEntity('foo');
4748
$proxy = $this->createProxy($entity, new NullLogger());
@@ -51,7 +52,7 @@ public function testToStringReturnsStringIfExceptionOccurredAndLoggerIsAvailable
5152
self::assertIsString($translation);
5253
}
5354

54-
public function testToStringLogsExceptionIfLoggerIsAvailable()
55+
public function testToStringLogsExceptionIfLoggerIsAvailable(): void
5556
{
5657
$entity = new TestEntity('foo');
5758

@@ -64,7 +65,7 @@ public function testToStringLogsExceptionIfLoggerIsAvailable()
6465
self::assertGreaterThan(0, \count($logger->cleanLogs()), 'Expected at least one log message');
6566
}
6667

67-
public function testLoggedMessageContainsInformationAboutTranslatedProperty()
68+
public function testLoggedMessageContainsInformationAboutTranslatedProperty(): void
6869
{
6970
$entity = new TestEntity('foo');
7071

@@ -84,7 +85,7 @@ public function testLoggedMessageContainsInformationAboutTranslatedProperty()
8485
self::assertStringContainsString('de', $logMessage, 'Missing locale.');
8586
}
8687

87-
public function testLoggedMessageContainsOriginalException()
88+
public function testLoggedMessageContainsOriginalException(): void
8889
{
8990
$entity = new TestEntity('foo');
9091

@@ -103,7 +104,7 @@ public function testLoggedMessageContainsOriginalException()
103104
}
104105

105106
/** @test */
106-
public function isTranslatedInto_returns_true_for_primary_translation_if_set()
107+
public function isTranslatedInto_returns_true_for_primary_translation_if_set(): void
107108
{
108109
$entity = new TestEntity('foo');
109110
$proxy = $this->createProxy($entity);
@@ -114,7 +115,7 @@ public function isTranslatedInto_returns_true_for_primary_translation_if_set()
114115
}
115116

116117
/** @test */
117-
public function isTranslatedInto_returns_true_for_translation_if_set()
118+
public function isTranslatedInto_returns_true_for_translation_if_set(): void
118119
{
119120
$entity = new TestEntity('foo');
120121
$proxy = $this->createProxy($entity);
@@ -125,7 +126,7 @@ public function isTranslatedInto_returns_true_for_translation_if_set()
125126
}
126127

127128
/** @test */
128-
public function isTranslatedInto_returns_false_if_primary_translation_is_empty()
129+
public function isTranslatedInto_returns_false_if_primary_translation_is_empty(): void
129130
{
130131
$entity = new TestEntity('foo');
131132
$proxy = $this->createProxy($entity);
@@ -137,7 +138,7 @@ public function isTranslatedInto_returns_false_if_primary_translation_is_empty()
137138
}
138139

139140
/** @test */
140-
public function isTranslatedInto_returns_false_if_translation_is_not_set()
141+
public function isTranslatedInto_returns_false_if_translation_is_not_set(): void
141142
{
142143
$entity = new TestEntity('foo');
143144
$proxy = $this->createProxy($entity);
@@ -147,10 +148,7 @@ public function isTranslatedInto_returns_false_if_translation_is_not_set()
147148
self::assertFalse($proxy->isTranslatedInto('fr'));
148149
}
149150

150-
/**
151-
* @return PersistentTranslatable
152-
*/
153-
private function createProxy(TestEntity $entity, ?LoggerInterface $logger = null)
151+
private function createProxy(TestEntity $entity, ?LoggerInterface $logger = null): PersistentTranslatable
154152
{
155153
$localeProvider = new DefaultLocaleProvider();
156154
$localeProvider->setDefaultLocale('de');
@@ -174,10 +172,10 @@ private function createProxy(TestEntity $entity, ?LoggerInterface $logger = null
174172
);
175173
}
176174

177-
private function breakEntity(TestEntity $entity)
175+
private function breakEntity(TestEntity $entity): void
178176
{
179-
$brokenCollection = $this->getMockBuilder('Doctrine\Common\Collections\ArrayCollection')->getMock();
180-
$brokenCollection->expects($this->any())
177+
$brokenCollection = $this->getMockBuilder(ArrayCollection::class)->getMock();
178+
$brokenCollection
181179
->method('matching')
182180
->will($this->throwException(new RuntimeException('Cannot find translations')));
183181
$property = new ReflectionProperty($entity, 'translations');

tests/Fixtures/Entity/TestEntity.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Webfactory\Bundle\PolyglotBundle\Tests\Fixtures\Entity;
1111

1212
use Doctrine\Common\Collections\ArrayCollection;
13+
use Doctrine\Common\Collections\Collection;
1314
use Doctrine\ORM\Mapping as ORM;
1415
use Webfactory\Bundle\PolyglotBundle\Attribute as Polyglot;
1516
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
@@ -36,7 +37,7 @@ class TestEntity
3637

3738
#[Polyglot\TranslationCollection]
3839
#[ORM\OneToMany(targetEntity: TestEntityTranslation::class, mappedBy: 'entity')] // This property is currently not typed to avoid an error in the \Webfactory\Bundle\PolyglotBundle\Tests\Doctrine\TranslatableClassMetadataTest::can_be_serialized_and_retrieved
39-
private $translations;
40+
private Collection $translations;
4041

4142
public function __construct($text)
4243
{

tests/Fixtures/Entity/TestEntityTranslation.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@ class TestEntityTranslation extends BaseTranslation
2828
* Contains the translation.
2929
*
3030
* Must be protected to be usable when this class is used as base for a mock.
31-
*
32-
* @var string
3331
*/
3432
#[ORM\Column(type: 'string')]
35-
protected $text;
33+
protected ?string $text;
3634

37-
/**
38-
* @param string|null $text
39-
*/
40-
public function __construct($locale = null, $text = null, ?TestEntity $entity = null)
35+
public function __construct($locale = null, ?string $text = null, ?TestEntity $entity = null)
4136
{
4237
$this->locale = $locale;
4338
$this->text = $text;

tests/Functional/TranslatingUnmappedPropertiesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* This test covers that also properties which are not mapped Doctrine fields
1313
* can be marked as translatable and will be handled by the PolyglotListener.
1414
*
15-
* This is useful when these fields are managed or updated by e. g. lifecycle callbacks
15+
* This is useful when these fields are managed or updated by e.g. lifecycle callbacks
1616
* or other Doctrine event listeners.
1717
*/
1818
class TranslatingUnmappedPropertiesTest extends FunctionalTestBase

tests/Functional/UndeclaredBaseClassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testUpdateTranslations(): void
8585

8686
/**
8787
* Fields in this class cannot be "private" - otherwise, they would not be picked up by the
88-
* Doctrine mapping drivers when processing the entity sub-class (UndeclaredBaseClassTest_EntityClass).
88+
* Doctrine mapping drivers when processing the entity subclass (UndeclaredBaseClassTest_EntityClass).
8989
*/
9090
class UndeclaredBaseClassTest_BaseClass
9191
{

0 commit comments

Comments
 (0)