Completed
Push — master ( 11ee78...8b3cbf )
by Thomas
04:46
created

StandardEnglishSingularizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 1
dl 12
loc 45
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getSingularForm() 12 42 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace keeko\core\utils;
3
4
use Propel\Common\Pluralizer\StandardEnglishPluralizer;
5
6
class StandardEnglishSingularizer extends StandardEnglishPluralizer {
7
8
	public function getSingularForm($plural) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
9
		// save some time in the case that singular and plural are the same
10
		if (in_array(strtolower($plural), $this->uncountable)) {
11
			return $plural;
12
		}
13
		
14
		// check for irregular singular words
15
		$irregular = array_flip($this->irregular);
16
		
17
		// some additions
18
		$irregular['indices'] = 'index';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
19
		$irregular['vertices'] = 'vertex';
20
		$irregular['matrices'] = 'matrix';
21
		
22 View Code Duplication
		foreach ($irregular as $pattern => $result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
			$searchPattern = '/' . $pattern . '$/i';
24
			if (preg_match($searchPattern, $plural)) {
25
				return preg_replace($searchPattern, $result, $plural);
26
			}
27
		}
28
		
29
		// check for irregular singular suffixes
30
		$plurals = array_flip($this->plural);
31
		
32
		// some modifications
33
		unset($plurals['\1ices']);
34
		unset($plurals['\1i']);
35
		unset($plurals['\1oes']);
36
		
37
		$plurals['(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)i'] = '\1us';
38
		$plurals['(buffal|tomat)oes'] = '\1o';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 49 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
39
		
40 View Code Duplication
		foreach ($plurals as $pattern => $result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
			$searchPattern = '/' . $pattern . '$/i';
42
			if (preg_match($searchPattern, $plural)) {
43
				return preg_replace($searchPattern, $result, $plural);
44
			}
45
		}
46
		
47
		// fallback to naive singularization
48
		return substr($plural, 0, -1);
49
	}
50
}