From f8d6df5156d87ed673032ca272af0a871509cbfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar?= Date: Sun, 17 Apr 2022 12:45:58 -0300 Subject: [PATCH] Coding standards --- .../GenericLanguageInflectorFactory.php | 31 ++++++++++--------- .../Inflector/LanguageInflectorFactory.php | 3 +- .../Inflector/InflectorWithIrregularsTest.php | 5 +-- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/Doctrine/Inflector/GenericLanguageInflectorFactory.php b/lib/Doctrine/Inflector/GenericLanguageInflectorFactory.php index 437bd83..be4ab36 100644 --- a/lib/Doctrine/Inflector/GenericLanguageInflectorFactory.php +++ b/lib/Doctrine/Inflector/GenericLanguageInflectorFactory.php @@ -4,14 +4,15 @@ namespace Doctrine\Inflector; -use Doctrine\Inflector\Rules\Ruleset; -use Doctrine\Inflector\Rules\Word; use Doctrine\Inflector\Rules\Patterns; +use Doctrine\Inflector\Rules\Ruleset; use Doctrine\Inflector\Rules\Substitution; use Doctrine\Inflector\Rules\Substitutions; use Doctrine\Inflector\Rules\Transformations; +use Doctrine\Inflector\Rules\Word; use function array_unshift; +use function is_array; abstract class GenericLanguageInflectorFactory implements LanguageInflectorFactory { @@ -65,27 +66,29 @@ final public function withPluralRules(?Ruleset $pluralRules, bool $reset = false return $this; } - final public function withIrregulars(array $irregulars, bool $reset = false): LanguageInflectorFactory + final public function withIrregulars(?array $irregulars, bool $reset = false): LanguageInflectorFactory { if ($reset) { $this->pluralRulesets = []; $this->singularRulesets = []; } - $newIrregulars = array(); - foreach ($irregulars as $irregular) { - $newIrregulars[] = new Substitution(new Word($irregular[0]), new Word($irregular[1])); - } + if (is_array($irregulars)) { + $newIrregulars = []; + foreach ($irregulars as $irregular) { + $newIrregulars[] = new Substitution(new Word($irregular[0]), new Word($irregular[1])); + } - $transf = new Transformations(); - $patterns = new Patterns(); - $substs = new Substitutions(...$newIrregulars); + $transf = new Transformations(); + $patterns = new Patterns(); + $substs = new Substitutions(...$newIrregulars); - $plural = new Ruleset($transf, $patterns, $substs); - $singular = new Ruleset($transf, $patterns, $substs->getFlippedSubstitutions()); + $plural = new Ruleset($transf, $patterns, $substs); + $singular = new Ruleset($transf, $patterns, $substs->getFlippedSubstitutions()); - array_unshift($this->pluralRulesets, $plural); - array_unshift($this->singularRulesets, $singular); + array_unshift($this->pluralRulesets, $plural); + array_unshift($this->singularRulesets, $singular); + } return $this; } diff --git a/lib/Doctrine/Inflector/LanguageInflectorFactory.php b/lib/Doctrine/Inflector/LanguageInflectorFactory.php index 92d2310..aa4de4c 100644 --- a/lib/Doctrine/Inflector/LanguageInflectorFactory.php +++ b/lib/Doctrine/Inflector/LanguageInflectorFactory.php @@ -29,11 +29,12 @@ public function withPluralRules(?Ruleset $pluralRules, bool $reset = false): sel /** * Applies custom rules for irregular words * + * @param mixed[][] $irregulars Array of arrays of strings in the format [['singular', 'plural'], ...] * @param bool $reset If true, will unset default inflections for all new rules * * @return $this */ - public function withIrregulars(array $irregulars, bool $reset = false): self; + public function withIrregulars(?array $irregulars, bool $reset = false): self; /** * Builds the inflector instance with all applicable rules diff --git a/tests/Doctrine/Tests/Inflector/InflectorWithIrregularsTest.php b/tests/Doctrine/Tests/Inflector/InflectorWithIrregularsTest.php index 1817c56..da60476 100644 --- a/tests/Doctrine/Tests/Inflector/InflectorWithIrregularsTest.php +++ b/tests/Doctrine/Tests/Inflector/InflectorWithIrregularsTest.php @@ -21,6 +21,7 @@ public function testIrregulars(string $word, string $expected): void self::assertSame($expected, $this->inflector->pluralize($word)); self::assertSame($word, $this->inflector->singularize($expected)); } + /** * @dataProvider dataRegulars */ @@ -57,12 +58,12 @@ public function dataIrregulars(): array // In the format array('word', 'expected') return [ ['foobar', 'barfoo'], - ['test', 'testz'] + ['test', 'testz'], ]; } protected function setUp(): void - { + { $this->inflector = InflectorFactory::create()->withIrregulars($this->dataIrregulars())->build(); } }