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