| Conditions | 1 |
| Paths | 1 |
| Total Lines | 105 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 51 | public function propertiesToBeTested() : array |
||
| 52 | { |
||
| 53 | $astLocator = (new BetterReflection())->astLocator(); |
||
| 54 | |||
| 55 | $fromLocator = new StringSourceLocator( |
||
| 56 | <<<'PHP' |
||
| 57 | <?php |
||
| 58 | |||
| 59 | class TheClass { |
||
| 60 | public $publicNothingToNothing; |
||
| 61 | public $publicNothingToNull; |
||
| 62 | public $publicNullToNull = null; |
||
| 63 | public $publicValueChanged = 1; |
||
| 64 | public $publicValueToSimilarValue = '1'; |
||
| 65 | public $publicExpressionToExpressionValue = 101 + 5; |
||
| 66 | |||
| 67 | protected $protectedNothingToNothing; |
||
| 68 | protected $protectedNothingToNull; |
||
| 69 | protected $protectedNullToNull = null; |
||
| 70 | protected $protectedValueChanged = 1; |
||
| 71 | protected $protectedValueToSimilarValue = '1'; |
||
| 72 | protected $protectedExpressionToExpressionValue = 101 + 5; |
||
| 73 | |||
| 74 | private $privateNothingToNothing; |
||
| 75 | private $privateNothingToNull; |
||
| 76 | private $privateNullToNull = null; |
||
| 77 | private $privateValueChanged = 1; |
||
| 78 | private $privateValueToSimilarValue = '1'; |
||
| 79 | private $privateExpressionToExpressionValue = 101 + 5; |
||
| 80 | } |
||
| 81 | PHP |
||
| 82 | , |
||
| 83 | $astLocator |
||
| 84 | ); |
||
| 85 | |||
| 86 | $toLocator = new StringSourceLocator( |
||
| 87 | <<<'PHP' |
||
| 88 | <?php |
||
| 89 | |||
| 90 | class TheClass { |
||
| 91 | public $publicNothingToNothing; |
||
| 92 | public $publicNothingToNull = null; |
||
| 93 | public $publicNullToNull = null; |
||
| 94 | public $publicValueChanged = 2; |
||
| 95 | public $publicValueToSimilarValue = 1; |
||
| 96 | public $publicExpressionToExpressionValue = 106; |
||
| 97 | |||
| 98 | protected $protectedNothingToNothing; |
||
| 99 | protected $protectedNothingToNull = null; |
||
| 100 | protected $protectedNullToNull = null; |
||
| 101 | protected $protectedValueChanged = 2; |
||
| 102 | protected $protectedValueToSimilarValue = 1; |
||
| 103 | protected $protectedExpressionToExpressionValue = 106; |
||
| 104 | |||
| 105 | private $privateNothingToNothing; |
||
| 106 | private $privateNothingToNull = null; |
||
| 107 | private $privateNullToNull = null; |
||
| 108 | private $privateValueChanged = 2; |
||
| 109 | private $privateValueToSimilarValue = 1; |
||
| 110 | private $privateExpressionToExpressionValue = 106; |
||
| 111 | } |
||
| 112 | PHP |
||
| 113 | , |
||
| 114 | $astLocator |
||
| 115 | ); |
||
| 116 | |||
| 117 | $fromClassReflector = new ClassReflector($fromLocator); |
||
| 118 | $toClassReflector = new ClassReflector($toLocator); |
||
| 119 | $fromClass = $fromClassReflector->reflect('TheClass'); |
||
| 120 | $toClass = $toClassReflector->reflect('TheClass'); |
||
| 121 | |||
| 122 | $properties = [ |
||
| 123 | 'publicNothingToNothing' => [], |
||
| 124 | 'publicNothingToNull' => [], |
||
| 125 | 'publicNullToNull' => [], |
||
| 126 | 'publicValueChanged' => ['[BC] CHANGED: Property TheClass#$publicValueChanged changed default value from 1 to 2'], |
||
| 127 | 'publicValueToSimilarValue' => ['[BC] CHANGED: Property TheClass#$publicValueToSimilarValue changed default value from \'1\' to 1'], |
||
| 128 | 'publicExpressionToExpressionValue' => [], |
||
| 129 | 'protectedNothingToNothing' => [], |
||
| 130 | 'protectedNothingToNull' => [], |
||
| 131 | 'protectedNullToNull' => [], |
||
| 132 | 'protectedValueChanged' => ['[BC] CHANGED: Property TheClass#$protectedValueChanged changed default value from 1 to 2'], |
||
| 133 | 'protectedValueToSimilarValue' => ['[BC] CHANGED: Property TheClass#$protectedValueToSimilarValue changed default value from \'1\' to 1'], |
||
| 134 | 'protectedExpressionToExpressionValue' => [], |
||
| 135 | 'privateNothingToNothing' => [], |
||
| 136 | 'privateNothingToNull' => [], |
||
| 137 | 'privateNullToNull' => [], |
||
| 138 | 'privateValueChanged' => ['[BC] CHANGED: Property TheClass#$privateValueChanged changed default value from 1 to 2'], |
||
| 139 | 'privateValueToSimilarValue' => ['[BC] CHANGED: Property TheClass#$privateValueToSimilarValue changed default value from \'1\' to 1'], |
||
| 140 | 'privateExpressionToExpressionValue' => [], |
||
| 141 | ]; |
||
| 142 | |||
| 143 | return TypeRestriction::array(array_combine( |
||
| 144 | array_keys($properties), |
||
| 145 | array_map( |
||
| 146 | /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */ |
||
| 147 | static function (string $property, array $errorMessages) use ($fromClass, $toClass) : array { |
||
| 148 | return [ |
||
| 149 | TypeRestriction::object($fromClass->getProperty($property)), |
||
| 150 | TypeRestriction::object($toClass->getProperty($property)), |
||
| 151 | $errorMessages, |
||
| 152 | ]; |
||
| 153 | }, |
||
| 154 | array_keys($properties), |
||
| 155 | $properties |
||
| 156 | ) |
||
| 160 |