| Conditions | 9 | 
| Paths | 19 | 
| Total Lines | 51 | 
| Code Lines | 25 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Tests | 24 | 
| CRAP Score | 9 | 
| 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 | ||
| 15 | public static function absoluteToRelative( | ||
| 16 | string $srcPath, | ||
| 17 | string $destPath | ||
| 18 |     ): string { | ||
| 19 | 1 |         if ($srcPath === $destPath) { | |
| 20 | 1 | return ''; | |
| 21 | } | ||
| 22 | |||
| 23 | //Remove first slash to not have a empty string for key 0 | ||
| 24 | //If path not start with slash, it's not an absolute path ! | ||
| 25 | 1 |         $srcPathEx  = explode('/', substr($srcPath, 1)); | |
| 26 | 1 |         $destPathEx = explode('/', substr(dirname($destPath), 1)); | |
| 27 | |||
| 28 | 1 | $samePathStatus = true; | |
| 29 | 1 | $notSameIdx = 0; | |
| 30 | 1 | $relativePath = ''; | |
| 31 | 1 | $endSrcPath = ''; | |
| 32 | |||
| 33 | 1 |         foreach ($srcPathEx as $srcIdx => $srcItem) { | |
| 34 | 1 |             if ($samePathStatus === true) { | |
| 35 | //Always in the same path | ||
| 36 | if ( | ||
| 37 | 1 | isset($destPathEx[$srcIdx]) | |
| 38 | 1 | && $srcItem === $destPathEx[$srcIdx] | |
| 39 |                 ) { | ||
| 40 | 1 | continue; | |
| 41 | } | ||
| 42 | |||
| 43 | 1 | $samePathStatus = false; | |
| 44 | 1 | $notSameIdx = $srcIdx; | |
| 45 | } | ||
| 46 | |||
| 47 | //Not the same path, so we add srcItem to the var which contain | ||
| 48 | //end of relative path which will be returned. | ||
| 49 | 1 | $endSrcPath .= (empty($endSrcPath)) ? '' : '/'; | |
| 50 | 1 | $endSrcPath .= $srcItem; | |
| 51 | } | ||
| 52 | |||
| 53 | //First item of paths is not same, no common between path. | ||
| 54 | 1 |         if ($notSameIdx === 0) { | |
| 55 | 1 | return $destPath; | |
| 56 | } | ||
| 57 | |||
| 58 | 1 | $nbDestItem = count($destPathEx) - 1; | |
| 59 | 1 |         for ($destIdx = $notSameIdx; $destIdx <= $nbDestItem; $destIdx++) { | |
| 60 | 1 | $relativePath .= '../'; | |
| 61 | } | ||
| 62 | |||
| 63 | 1 | $relativePath .= $endSrcPath; | |
| 64 | |||
| 65 | 1 | return $relativePath; | |
| 66 | } | ||
| 68 |