Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimum PHP version supported bumped to 7.1, adding related static analysis improvements #50

Merged
merged 4 commits into from
Jul 22, 2017
Merged
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ cache:
- $HOME/.composer/cache

php:
- 7.0
- 7.1
- nightly

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{"name": "Johannes Schmitt", "email": "schmittjoh@gmail.com"}
],
"require": {
"php": "^7.0"
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^6.2"
Expand Down
52 changes: 19 additions & 33 deletions lib/Doctrine/Common/Inflector/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Inflector
/**
* Plural inflector rules.
*
* @var array
* @var string[][]
*/
private static $plural = array(
'rules' => array(
Expand Down Expand Up @@ -145,7 +145,7 @@ class Inflector
/**
* Singular inflector rules.
*
* @var array
* @var string[][]
*/
private static $singular = array(
'rules' => array(
Expand Down Expand Up @@ -250,36 +250,24 @@ class Inflector

/**
* Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'.
*
* @param string $word The word to tableize.
*
* @return string The tableized word.
*/
public static function tableize($word)
public static function tableize(string $word) : string
{
return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word));
}

/**
* Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'.
*
* @param string $word The word to classify.
*
* @return string The classified word.
*/
public static function classify($word)
public static function classify(string $word) : string
{
return str_replace(' ', '', ucwords(strtr($word, '_-', ' ')));
}

/**
* Camelizes a word. This uses the classify() method and turns the first character to lowercase.
*
* @param string $word The word to camelize.
*
* @return string The camelized word.
*/
public static function camelize($word)
public static function camelize(string $word) : string
{
return lcfirst(self::classify($word));
}
Expand Down Expand Up @@ -309,7 +297,7 @@ public static function camelize($word)
*
* @return string The string with all delimeter-separated words capitalized.
*/
public static function ucwords($string, $delimiters = " \n\t\r\0\x0B-")
public static function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string
{
return preg_replace_callback(
'/[^' . preg_quote($delimiters, '/') . ']+/',
Expand All @@ -323,10 +311,8 @@ function($matches) {
/**
* Clears Inflectors inflected value caches, and resets the inflection
* rules to the initial values.
*
* @return void
*/
public static function reset()
public static function reset() : void
{
if (empty(self::$initialState)) {
self::$initialState = get_class_vars('Inflector');
Expand All @@ -335,7 +321,7 @@ public static function reset()
}

foreach (self::$initialState as $key => $val) {
if ($key != 'initialState') {
if ($key !== 'initialState') {
self::${$key} = $val;
}
}
Expand All @@ -355,14 +341,14 @@ public static function reset()
* ));
* }}}
*
* @param string $type The type of inflection, either 'plural' or 'singular'
* @param array $rules An array of rules to be added.
* @param boolean $reset If true, will unset default inflections for all
* new rules that are being defined in $rules.
* @param string $type The type of inflection, either 'plural' or 'singular'
* @param array|iterable $rules An array of rules to be added.
* @param boolean $reset If true, will unset default inflections for all
* new rules that are being defined in $rules.
*
* @return void
*/
public static function rules($type, $rules, $reset = false)
public static function rules(string $type, iterable $rules, bool $reset = false) : void
{
foreach ($rules as $rule => $pattern) {
if ( ! is_array($pattern)) {
Expand Down Expand Up @@ -400,7 +386,7 @@ public static function rules($type, $rules, $reset = false)
*
* @return string The word in plural form.
*/
public static function pluralize($word)
public static function pluralize(string $word) : string
{
if (isset(self::$cache['pluralize'][$word])) {
return self::$cache['pluralize'][$word];
Expand All @@ -420,7 +406,7 @@ public static function pluralize($word)
}

if (preg_match('/(.*)\\b(' . self::$plural['cacheIrregular'] . ')$/i', $word, $regs)) {
self::$cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$plural['merged']['irregular'][strtolower($regs[2])], 1);
self::$cache['pluralize'][$word] = $regs[1] . $word[0] . substr(self::$plural['merged']['irregular'][strtolower($regs[2])], 1);

return self::$cache['pluralize'][$word];
}
Expand All @@ -447,7 +433,7 @@ public static function pluralize($word)
*
* @return string The word in singular form.
*/
public static function singularize($word)
public static function singularize(string $word) : string
{
if (isset(self::$cache['singularize'][$word])) {
return self::$cache['singularize'][$word];
Expand All @@ -468,12 +454,12 @@ public static function singularize($word)
}

if (!isset(self::$singular['cacheUninflected']) || !isset(self::$singular['cacheIrregular'])) {
self::$singular['cacheUninflected'] = '(?:' . join('|', self::$singular['merged']['uninflected']) . ')';
self::$singular['cacheIrregular'] = '(?:' . join('|', array_keys(self::$singular['merged']['irregular'])) . ')';
self::$singular['cacheUninflected'] = '(?:' . implode('|', self::$singular['merged']['uninflected']) . ')';
self::$singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$singular['merged']['irregular'])) . ')';
}

if (preg_match('/(.*)\\b(' . self::$singular['cacheIrregular'] . ')$/i', $word, $regs)) {
self::$cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$singular['merged']['irregular'][strtolower($regs[2])], 1);
self::$cache['singularize'][$word] = $regs[1] . $word[0] . substr(self::$singular['merged']['irregular'][strtolower($regs[2])], 1);

return self::$cache['singularize'][$word];
}
Expand Down
86 changes: 19 additions & 67 deletions tests/Doctrine/Tests/Common/Inflector/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class InflectorTest extends TestCase
/**
* Singular & Plural test data. Returns an array of sample words.
*
* @return array
* @return string[][]
*/
public function dataSampleWords()
public function dataSampleWords() : array
{
Inflector::reset();

Expand Down Expand Up @@ -157,12 +157,9 @@ public function dataSampleWords()
}

/**
* testInflectingSingulars method
*
* @dataProvider dataSampleWords
* @return void
*/
public function testInflectingSingulars($singular, $plural)
public function testInflectingSingulars(string $singular, string $plural) : void
{
$this->assertEquals(
$singular,
Expand All @@ -172,12 +169,9 @@ public function testInflectingSingulars($singular, $plural)
}

/**
* testInflectingPlurals method
*
* @dataProvider dataSampleWords
* @return void
*/
public function testInflectingPlurals($singular, $plural)
public function testInflectingPlurals(string $singular, string $plural) : void
{
$this->assertEquals(
$plural,
Expand All @@ -186,12 +180,7 @@ public function testInflectingPlurals($singular, $plural)
);
}

/**
* testCustomPluralRule method
*
* @return void
*/
public function testCustomPluralRule()
public function testCustomPluralRule() : void
{
Inflector::reset();
Inflector::rules('plural', array('/^(custom)$/i' => '\1izables'));
Expand All @@ -215,12 +204,7 @@ public function testCustomPluralRule()
$this->assertEquals(Inflector::pluralize('phone'), 'phonezes');
}

/**
* testCustomSingularRule method
*
* @return void
*/
public function testCustomSingularRule()
public function testCustomSingularRule() : void
{
Inflector::reset();
Inflector::rules('singular', array('/(eple)r$/i' => '\1', '/(jente)r$/i' => '\1'));
Expand All @@ -240,12 +224,7 @@ public function testCustomSingularRule()
$this->assertEquals(Inflector::singularize('singulars'), 'singulars');
}

/**
* test that setting new rules clears the inflector caches.
*
* @return void
*/
public function testRulesClearsCaches()
public function testSettingNewRulesClearsCaches() : void
{
Inflector::reset();

Expand All @@ -267,12 +246,7 @@ public function testRulesClearsCaches()
$this->assertEquals(Inflector::pluralize('corpus'), 'corpora', 'Was inflected with old irregular form.');
}

/**
* Test resetting inflection rules.
*
* @return void
*/
public function testCustomRuleWithReset()
public function testCustomRuleWithReset() : void
{
Inflector::reset();

Expand All @@ -298,44 +272,30 @@ public function testCustomRuleWithReset()
$this->assertEquals(Inflector::singularize('Atlas'), 'Atlas');
}

/**
* Test basic ucwords functionality.
*
* @return void
*/
public function testUcwords()
public function testUcwords() : void
{
$this->assertSame('Top-O-The-Morning To All_of_you!', Inflector::ucwords( 'top-o-the-morning to all_of_you!'));
}

/**
* Test ucwords functionality with custom delimeters.
*
* @return void
*/
public function testUcwordsWithCustomDelimeters()
public function testUcwordsWithCustomDelimeters() : void
{
$this->assertSame('Top-O-The-Morning To All_Of_You!', Inflector::ucwords( 'top-o-the-morning to all_of_you!', '-_ '));
}

/**
* @param $expected
* @param $word
*
* @dataProvider dataStringsTableize
* @return void
*/
public function testTableize($expected, $word)
public function testTableize(string $expected, string $word) : void
{
$this->assertSame($expected, Inflector::tableize($word));
}

/**
* Strings which are used for testTableize.
*
* @return array
* @return string[][]
*/
public function dataStringsTableize()
public function dataStringsTableize() : array
{
// In the format array('expected', 'word')
return array(
Expand All @@ -346,23 +306,19 @@ public function dataStringsTableize()
}

/**
* @param $expected
* @param $word
*
* @dataProvider dataStringsClassify
* @return void
*/
public function testClassify($expected, $word)
public function testClassify(string $expected, string $word) : void
{
$this->assertSame($expected, Inflector::classify($word));
}

/**
* Strings which are used for testClassify.
*
* @return array
* @return string[][]
*/
public function dataStringsClassify()
public function dataStringsClassify() : array
{
// In the format array('expected', 'word')
return array(
Expand All @@ -376,23 +332,19 @@ public function dataStringsClassify()
}

/**
* @param $expected
* @param $word
*
* @dataProvider dataStringsCamelize
* @return void
*/
public function testCamelize($expected, $word)
public function testCamelize(string $expected, string $word) : void
{
$this->assertSame($expected, Inflector::camelize($word));
}

/**
* Strings which are used for testCamelize.
*
* @return array
* @return string[][]
*/
public function dataStringsCamelize()
public function dataStringsCamelize() : array
{
// In the format array('expected', 'word')
return array(
Expand Down