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  | 
            ||
| 28 | class ResourceStack  | 
            ||
| 29 | { | 
            ||
| 30 | /**  | 
            ||
| 31 | * @var array  | 
            ||
| 32 | */  | 
            ||
| 33 | private $stack;  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * @param array $stack  | 
            ||
| 37 | */  | 
            ||
| 38 | 28 | public function __construct($stack)  | 
            |
| 39 |     { | 
            ||
| 40 | 28 | Assert::isArray($stack, 'Built resource stack must be an array.');  | 
            |
| 41 | |||
| 42 | 27 | $this->stack = $stack;  | 
            |
| 43 | 27 | }  | 
            |
| 44 | |||
| 45 | /**  | 
            ||
| 46 | * Get the current version resource.  | 
            ||
| 47 | *  | 
            ||
| 48 | * @return PuliResource  | 
            ||
| 49 | */  | 
            ||
| 50 | 10 | public function getCurrent()  | 
            |
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * Get the current version number.  | 
            ||
| 57 | *  | 
            ||
| 58 | * @return int  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 59 | */  | 
            ||
| 60 | 12 | View Code Duplication | public function getCurrentVersion()  | 
            
| 61 |     { | 
            ||
| 62 | 12 | $versions = $this->getVersions();  | 
            |
| 63 | |||
| 64 | 12 |         if (0 === count($versions)) { | 
            |
| 65 | 2 |             throw new RuntimeException('Could not retrieve the current version of an empty stack.'); | 
            |
| 66 | }  | 
            ||
| 67 | |||
| 68 | 10 | return end($versions);  | 
            |
| 69 | }  | 
            ||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * Get the current version resource.  | 
            ||
| 73 | *  | 
            ||
| 74 | * @return PuliResource  | 
            ||
| 75 | */  | 
            ||
| 76 | 10 | public function getFirst()  | 
            |
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * Get the first version number.  | 
            ||
| 83 | *  | 
            ||
| 84 | * @return int  | 
            ||
| 85 | */  | 
            ||
| 86 | 12 | View Code Duplication | public function getFirstVersion()  | 
            
| 87 |     { | 
            ||
| 88 | 12 | $versions = $this->getVersions();  | 
            |
| 89 | |||
| 90 | 12 |         if (0 === count($versions)) { | 
            |
| 91 | 2 |             throw new RuntimeException('Could not retrieve the first version of an empty stack.'); | 
            |
| 92 | }  | 
            ||
| 93 | |||
| 94 | 10 | return reset($versions);  | 
            |
| 95 | }  | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * Get a specific version resource from the stack by its version number.  | 
            ||
| 99 | *  | 
            ||
| 100 | * @param int $version The version number (first is 0).  | 
            ||
| 101 | *  | 
            ||
| 102 | * @return PuliResource  | 
            ||
| 103 | */  | 
            ||
| 104 | 15 | public function get($version)  | 
            |
| 119 | |||
| 120 | /**  | 
            ||
| 121 | * Get an array of the available versions of this resource.  | 
            ||
| 122 | *  | 
            ||
| 123 | * @return array  | 
            ||
| 124 | */  | 
            ||
| 125 | 23 | public function getVersions()  | 
            |
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * Returns the stack contents as array.  | 
            ||
| 132 | *  | 
            ||
| 133 | * @return PuliResource[] The resources in the stack.  | 
            ||
| 134 | */  | 
            ||
| 135 | public function toArray()  | 
            ||
| 139 | }  | 
            ||
| 140 | 
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.