| Conditions | 6 |
| Paths | 14 |
| Total Lines | 76 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 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 |
||
| 37 | protected function getPackageBasePath(PackageInterface $package) |
||
| 38 | { |
||
| 39 | if ($this->composer->getPackage()->getPrettyName() === 'simplesamlphp/simplesamlphp') { |
||
| 40 | $ssp_path = "."; |
||
| 41 | } else { |
||
| 42 | $ssp_path = $this->composer->getConfig()->get('vendor-dir') . '/simplesamlphp/simplesamlphp'; |
||
| 43 | } |
||
| 44 | |||
| 45 | $matches = []; |
||
| 46 | $name = $package->getPrettyName(); |
||
| 47 | if (!preg_match('@^.*/simplesamlphp-(module|assets)-(.+)$@', $name, $matches)) { |
||
| 48 | throw new InvalidArgumentException(sprintf( |
||
| 49 | 'Unable to install module %s,' |
||
| 50 | . ' package name must be in the form "VENDOR/simplesamlphp-(module|assets)-MODULENAME".', |
||
| 51 | $name, |
||
| 52 | )); |
||
| 53 | } |
||
| 54 | |||
| 55 | Assert::count($matches, 3); |
||
| 56 | $moduleType = $matches[1]; |
||
| 57 | $moduleDir = $matches[2]; |
||
| 58 | |||
| 59 | Assert::regex( |
||
| 60 | $moduleDir, |
||
| 61 | '@^[a-z0-9_.-]*$@', |
||
| 62 | sprintf( |
||
| 63 | 'Unable to install module %s,' |
||
| 64 | . ' module name must only contain characters from a-z, 0-9, "_", "." and "-".', |
||
| 65 | $name |
||
| 66 | ), |
||
| 67 | InvalidArgumentException::class |
||
| 68 | ); |
||
| 69 | |||
| 70 | Assert::notStartsWith( |
||
| 71 | $moduleDir, |
||
| 72 | '.', |
||
| 73 | sprintf('Unable to install module %s, module name cannot start with ".".', $name), |
||
| 74 | InvalidArgumentException::class |
||
| 75 | ); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Composer packages are supposed to only contain lowercase letters, |
||
| 79 | * but historically many modules have had names in mixed case. |
||
| 80 | * We must provide a way to handle those. Here we allow the module directory |
||
| 81 | * to be overridden with a mixed case name. |
||
| 82 | */ |
||
| 83 | $extraData = $package->getExtra(); |
||
| 84 | if (isset($extraData[self::MIXED_CASE])) { |
||
| 85 | $mixedCaseModuleName = $extraData[self::MIXED_CASE]; |
||
| 86 | Assert::string( |
||
| 87 | $mixedCaseModuleName, |
||
| 88 | sprintf('Unable to install module %s, "%s" must be a string.', $name, self::MIXED_CASE), |
||
| 89 | InvalidArgumentException::class |
||
| 90 | ); |
||
| 91 | |||
| 92 | Assert::same( |
||
| 93 | mb_strtolower($mixedCaseModuleName, 'utf-8'), |
||
| 94 | $moduleDir, |
||
| 95 | sprintf( |
||
| 96 | 'Unable to install module %s,' |
||
| 97 | . ' "%s" must match the package name except that it can contain uppercase letters.', |
||
| 98 | $name, |
||
| 99 | self::MIXED_CASE |
||
| 100 | ), |
||
| 101 | InvalidArgumentException::class |
||
| 102 | ); |
||
| 103 | $moduleDir = $mixedCaseModuleName; |
||
| 104 | } |
||
| 105 | |||
| 106 | switch ($moduleType) { |
||
| 107 | case 'assets': |
||
| 108 | return $ssp_path . '/public/assets/' . $moduleDir; |
||
| 109 | case 'module': |
||
| 110 | return $ssp_path . '/modules/' . $moduleDir; |
||
| 111 | default: |
||
| 112 | throw new InvalidArgumentException(sprintf('Unsupported type: %s', $moduleType)); |
||
| 113 | } |
||
| 125 |