From d1e672bb19fa9dd31f4ae20d2cc12942e8bf709b Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 01:24:28 +0100 Subject: [PATCH] Trigger deprecation notices in Inflector and reuse the Doctrine inflector Inflector is marked @deprecated 5.0 but gave users no notice before the removal. rules() and reset() are empty methods kept for the former interface, so custom inflection rules were accepted and silently ignored. Every call also built a new Doctrine inflector. Trigger a deprecation notice in every public method, with a specific message for rules() and reset(), and build the Doctrine inflector only once. Fix the upgrade guide, which pointed to the Doctrine\Common\Inflector API that doctrine/inflector 2.x no longer has. Co-Authored-By: Claude Opus 5.5 --- Tests/InflectorTest.php | 52 +++++++++++++++++++++ docs/v3-to-v4-update.md | 39 ++++++++++++++-- src/Inflector.php | 101 +++++++++++++++++++++++++++++++++++----- 3 files changed, 175 insertions(+), 17 deletions(-) diff --git a/Tests/InflectorTest.php b/Tests/InflectorTest.php index fd708013..aca2923b 100644 --- a/Tests/InflectorTest.php +++ b/Tests/InflectorTest.php @@ -246,4 +246,56 @@ public function testSingularizeAlreadySingular() "'glass' should not be singularised'" ); } + + /** + * @testdox Using the deprecated Inflector class triggers a deprecation notice + */ + public function testPluralizeTriggersDeprecation() + { + $this->expectUserDeprecationMessage( + 'Since joomla/string __DEPLOY_VERSION__: The Joomla\String\Inflector class is deprecated and will be removed in 5.0.' + . ' Use the doctrine/inflector package instead.' + ); + + $this->assertSame('houses', Inflector::pluralize('house')); + } + + /** + * @testdox rules() has no effect and triggers a deprecation notice + */ + public function testRulesTriggersDeprecation() + { + $this->expectUserDeprecationMessage( + 'Since joomla/string __DEPLOY_VERSION__: Joomla\String\Inflector::rules() has no effect and will be removed in 5.0.' + . ' Configure the rules of doctrine/inflector with InflectorFactory::create()->withSingularRules() and withPluralRules() instead.' + ); + + Inflector::rules('plural', ['/^house$/i' => 'housen']); + + $this->assertSame('houses', Inflector::pluralize('house')); + } + + /** + * @testdox reset() has no effect and triggers a deprecation notice + */ + public function testResetTriggersDeprecation() + { + $this->expectUserDeprecationMessage( + 'Since joomla/string __DEPLOY_VERSION__: Joomla\String\Inflector::reset() has no effect and will be removed in 5.0.' + . ' Configure the rules of doctrine/inflector with InflectorFactory::create()->withSingularRules() and withPluralRules() instead.' + ); + + Inflector::reset(); + } + + /** + * @testdox The Doctrine inflector is built once and reused + */ + public function testDoctrineInflectorIsReused() + { + $this->assertSame( + TestHelper::invoke($this->inflector, 'getInflector'), + TestHelper::invoke($this->inflector, 'getInflector') + ); + } } diff --git a/docs/v3-to-v4-update.md b/docs/v3-to-v4-update.md index 5abbd7bd..49f7788f 100644 --- a/docs/v3-to-v4-update.md +++ b/docs/v3-to-v4-update.md @@ -8,13 +8,42 @@ All Framework packages now require PHP 8.3 or newer. The following deprecated methods have been removed in this release: -* `Inflector::addWord()`: Use `Doctrine\Common\Inflector\Inflector::rules()` -* `Inflector::addPluraliseRule()`: Use `Doctrine\Common\Inflector\Inflector::rules()` -* `Inflector::addSingulariseRule()`: Use `Doctrine\Common\Inflector\Inflector::rules()` +* `Inflector::addWord()`: Configure an irregular word for `doctrine/inflector` with `Doctrine\Inflector\InflectorFactory::create()->withSingularRules()` and `->withPluralRules()` +* `Inflector::addPluraliseRule()`: Use `Doctrine\Inflector\InflectorFactory::create()->withPluralRules()` +* `Inflector::addSingulariseRule()`: Use `Doctrine\Inflector\InflectorFactory::create()->withSingularRules()` * `Inflector::getInstance()`: Use the static methods without an instance instead. -* `Inflector::toPlural()`: Use static `Doctrine\Common\Inflector\Inflector::pluralize()` -* `Inflector::toSingular()`: Use static `Doctrine\Common\Inflector\Inflector::singularize()` +* `Inflector::toPlural()`: Use `pluralize()` of a `Doctrine\Inflector\Inflector` built with `Doctrine\Inflector\InflectorFactory::create()->build()` +* `Inflector::toSingular()`: Use `singularize()` of a `Doctrine\Inflector\Inflector` built with `Doctrine\Inflector\InflectorFactory::create()->build()` + +The `rules()` and `reset()` methods of `Joomla\String\Inflector` are kept only for the former interface and have +no effect: rules passed to them are ignored. They trigger a deprecation notice. Configure the rules of +`doctrine/inflector` instead: + +```php +use Doctrine\Inflector\InflectorFactory; +use Doctrine\Inflector\Rules\Pattern; +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; + +$inflector = InflectorFactory::create() + ->withPluralRules( + new Ruleset( + new Transformations(), + new Patterns(new Pattern('sheep')), + new Substitutions(new Substitution(new Word('person'), new Word('folk'))) + ) + ) + ->build(); + +$inflector->pluralize('person'); // 'folk' +``` ### The `Inflector` class has been deprecated The `Inflector` class in this package does not provide benefits over the original `doctrine/inflector` package anymore and is currently only there to keep backwards compatibility with version 3. Use `doctrine/inflector` directly instead. This class will be removed in 5.0 of this package. + +Every method of the class triggers a deprecation notice (`E_USER_DEPRECATED` through `symfony/deprecation-contracts`). diff --git a/src/Inflector.php b/src/Inflector.php index 4f99f0fb..81e84534 100644 --- a/src/Inflector.php +++ b/src/Inflector.php @@ -9,6 +9,7 @@ namespace Joomla\String; +use Doctrine\Inflector\Inflector as DoctrineInflector; use Doctrine\Inflector\InflectorFactory; /** @@ -35,6 +36,14 @@ class Inflector ], ]; + /** + * The Doctrine inflector the proxy methods use. + * + * @var DoctrineInflector|null + * @since __DEPLOY_VERSION__ + */ + private static $inflector; + /** * Adds inflection regex rules to the inflector. * @@ -66,6 +75,56 @@ private function addRule($data, string $ruleType) } } + /** + * Returns the Doctrine inflector, building it on first use. + * + * @return DoctrineInflector + * + * @since __DEPLOY_VERSION__ + */ + private static function getInflector(): DoctrineInflector + { + return self::$inflector ??= InflectorFactory::create()->build(); + } + + /** + * Triggers the deprecation notice for the Inflector class. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + private static function triggerClassDeprecation(): void + { + trigger_deprecation( + 'joomla/string', + '__DEPLOY_VERSION__', + 'The %s class is deprecated and will be removed in 5.0. Use the doctrine/inflector package instead.', + self::class + ); + } + + /** + * Triggers the deprecation notice for the inflection rule methods, which have no effect. + * + * @param string $method The method that was called + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + private static function triggerRulesDeprecation(string $method): void + { + trigger_deprecation( + 'joomla/string', + '__DEPLOY_VERSION__', + '%s::%s() has no effect and will be removed in 5.0. Configure the rules of doctrine/inflector with' + . ' InflectorFactory::create()->withSingularRules() and withPluralRules() instead.', + self::class, + $method + ); + } + /** * Adds a countable word. * @@ -77,6 +136,8 @@ private function addRule($data, string $ruleType) */ public function addCountableRule($data) { + self::triggerClassDeprecation(); + $this->addRule($data, 'countable'); return $this; @@ -93,6 +154,8 @@ public function addCountableRule($data) */ public function isCountable($word) { + self::triggerClassDeprecation(); + return \in_array($word, self::$countable['rules']); } @@ -107,7 +170,11 @@ public function isCountable($word) */ public function isPlural($word) { - return static::pluralize(static::singularize($word)) === $word; + self::triggerClassDeprecation(); + + $inflector = self::getInflector(); + + return $inflector->pluralize($inflector->singularize($word)) === $word; } /** @@ -121,7 +188,9 @@ public function isPlural($word) */ public function isSingular($word) { - return static::singularize($word) === $word; + self::triggerClassDeprecation(); + + return self::getInflector()->singularize($word) === $word; } /** @@ -129,9 +198,9 @@ public function isSingular($word) */ public static function tableize(string $word): string { - $inflector = InflectorFactory::create()->build(); + self::triggerClassDeprecation(); - return $inflector->tableize($word); + return self::getInflector()->tableize($word); } /** @@ -139,9 +208,9 @@ public static function tableize(string $word): string */ public static function classify(string $word): string { - $inflector = InflectorFactory::create()->build(); + self::triggerClassDeprecation(); - return $inflector->classify($word); + return self::getInflector()->classify($word); } /** @@ -149,9 +218,9 @@ public static function classify(string $word): string */ public static function camelize(string $word): string { - $inflector = InflectorFactory::create()->build(); + self::triggerClassDeprecation(); - return $inflector->camelize($word); + return self::getInflector()->camelize($word); } /** @@ -159,21 +228,29 @@ public static function camelize(string $word): string */ public static function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-"): string { + self::triggerClassDeprecation(); + return ucwords($string, $delimiters); } /** * Empty method to suffice the former interface + * + * @deprecated 5.0 Has no effect. Configure the rules of doctrine/inflector with InflectorFactory instead. */ public static function reset(): void { + self::triggerRulesDeprecation('reset'); } /** * Empty method to suffice the former interface + * + * @deprecated 5.0 Has no effect. Configure the rules of doctrine/inflector with InflectorFactory instead. */ public static function rules(string $type, iterable $rules, bool $reset = false): void { + self::triggerRulesDeprecation('rules'); } /** @@ -181,9 +258,9 @@ public static function rules(string $type, iterable $rules, bool $reset = false) */ public static function pluralize(string $word): string { - $inflector = InflectorFactory::create()->build(); + self::triggerClassDeprecation(); - return $inflector->pluralize($word); + return self::getInflector()->pluralize($word); } /** @@ -191,8 +268,8 @@ public static function pluralize(string $word): string */ public static function singularize(string $word): string { - $inflector = InflectorFactory::create()->build(); + self::triggerClassDeprecation(); - return $inflector->singularize($word); + return self::getInflector()->singularize($word); } }