Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Tests/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}
}
39 changes: 34 additions & 5 deletions docs/v3-to-v4-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
101 changes: 89 additions & 12 deletions src/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Joomla\String;

use Doctrine\Inflector\Inflector as DoctrineInflector;
use Doctrine\Inflector\InflectorFactory;

/**
Expand All @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -77,6 +136,8 @@ private function addRule($data, string $ruleType)
*/
public function addCountableRule($data)
{
self::triggerClassDeprecation();

$this->addRule($data, 'countable');

return $this;
Expand All @@ -93,6 +154,8 @@ public function addCountableRule($data)
*/
public function isCountable($word)
{
self::triggerClassDeprecation();

return \in_array($word, self::$countable['rules']);
}

Expand All @@ -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;
}

/**
Expand All @@ -121,78 +188,88 @@ public function isPlural($word)
*/
public function isSingular($word)
{
return static::singularize($word) === $word;
self::triggerClassDeprecation();

return self::getInflector()->singularize($word) === $word;
}

/**
* Proxy for Inflector::tableize()
*/
public static function tableize(string $word): string
{
$inflector = InflectorFactory::create()->build();
self::triggerClassDeprecation();

return $inflector->tableize($word);
return self::getInflector()->tableize($word);
}

/**
* Proxy for Inflector::classify()
*/
public static function classify(string $word): string
{
$inflector = InflectorFactory::create()->build();
self::triggerClassDeprecation();

return $inflector->classify($word);
return self::getInflector()->classify($word);
}

/**
* Proxy for Inflector::camelize()
*/
public static function camelize(string $word): string
{
$inflector = InflectorFactory::create()->build();
self::triggerClassDeprecation();

return $inflector->camelize($word);
return self::getInflector()->camelize($word);
}

/**
* Proxy for Inflector::ucwords()
*/
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');
}

/**
* Proxy for Inflector::pluralize()
*/
public static function pluralize(string $word): string
{
$inflector = InflectorFactory::create()->build();
self::triggerClassDeprecation();

return $inflector->pluralize($word);
return self::getInflector()->pluralize($word);
}

/**
* Proxy for Inflector::singularize()
*/
public static function singularize(string $word): string
{
$inflector = InflectorFactory::create()->build();
self::triggerClassDeprecation();

return $inflector->singularize($word);
return self::getInflector()->singularize($word);
}
}
Loading