Conditions | 7 |
Paths | 13 |
Total Lines | 55 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 1 |
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 namespace Mascame\Artificer\Extension; |
||
61 | public function add($package, $plugins) |
||
62 | { |
||
63 | if (! $this->isValidPackageName($package)) { |
||
64 | throw new \Exception('Extension namespace is mandatory and must be compliant to "vendor/package". Provided: ' . $package); |
||
65 | } |
||
66 | |||
67 | // Convert to array |
||
68 | if (! is_array($plugins)) $plugins = [$plugins]; |
||
69 | |||
70 | // Get package info |
||
71 | if (! isset(self::$packages[$package])) { |
||
72 | self::$packages[$package] = new \stdClass(); |
||
73 | |||
74 | if (isset($this->composerPackages[$package])) { |
||
75 | $packageData = $this->composerPackages[$package]; |
||
76 | } else { |
||
77 | $packageData = [ |
||
78 | 'name' => $package, |
||
79 | 'version' => 'none', |
||
80 | 'description' => null, |
||
81 | 'authors' => [ |
||
82 | [ |
||
83 | 'name' => 'Anonymous', |
||
84 | 'email' => '[email protected]' |
||
85 | ] |
||
86 | ] |
||
87 | ]; |
||
88 | } |
||
89 | |||
90 | self::$packages[$package] = (object) $packageData; |
||
91 | } |
||
92 | |||
93 | foreach ($plugins as $pluginName) { |
||
94 | // Group the extensions provided under the package namespace |
||
95 | self::$packages[$package]->provides[$this->type][] = $pluginName; |
||
96 | |||
97 | parent::add($pluginName, function() use ($pluginName, $package) { |
||
98 | |||
99 | /** |
||
100 | * First we try to resolve the plugin within the App Container |
||
101 | */ |
||
102 | try { |
||
103 | $plugin = app($pluginName); |
||
104 | } catch (\ReflectionException $e) { |
||
105 | $plugin = new $pluginName; |
||
106 | } |
||
107 | |||
108 | $plugin->package = $package; |
||
109 | |||
110 | return $plugin; |
||
111 | }); |
||
112 | } |
||
113 | |||
114 | return true; |
||
115 | } |
||
116 | |||
164 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: