Total Complexity | 45 |
Total Lines | 173 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 0 |
Complex classes like Loader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Loader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class Loader implements LoaderInterface |
||
49 | { |
||
50 | protected $versionParser; |
||
51 | |||
52 | protected $loadOptions; |
||
53 | |||
54 | public function __construct(?VersionParser $parser = null, $loadOptions = false) |
||
55 | { |
||
56 | $this->versionParser = $parser ?: new VersionParser(); |
||
57 | $this->loadOptions = $loadOptions; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $package |
||
62 | * |
||
63 | * @return \Pickle\Base\Interfaces\Package $package |
||
64 | */ |
||
65 | public function load(array $config, $package = 'Pickle\Base\Interfaces\Package') |
||
66 | { |
||
67 | if (isset($config['version'])) { |
||
68 | $version = $this->versionParser->normalize($config['version']); |
||
69 | $package = Package::factory($config['name'], $version, $config['version'], true); |
||
70 | } else { |
||
71 | $package = Package::factory($config['name'], '', '', true); |
||
72 | } |
||
73 | |||
74 | if (isset($config['type']) && $config['type'] != 'extension') { |
||
75 | throw new UnexpectedValueException($package->getName() . ' is not a extension(s) package'); |
||
76 | } |
||
77 | $package->setType('extension'); |
||
78 | |||
79 | $this->setPackageSource($package, $config); |
||
80 | $this->setPackageDist($package, $config); |
||
81 | $this->setPackageReleaseDate($package, $config); |
||
82 | $this->setPackageStability($package, $config); |
||
83 | $this->setPackageExtra($package, $config); |
||
84 | $this->setPackageDescription($package, $config); |
||
85 | $this->setPackageHomepage($package, $config); |
||
86 | $this->setPackageKeywords($package, $config); |
||
87 | $this->setPackageLicense($package, $config); |
||
88 | $this->setPackageAuthors($package, $config); |
||
89 | $this->setPackageSupport($package, $config); |
||
90 | |||
91 | return $package; |
||
92 | } |
||
93 | |||
94 | protected function setPackageStability(Interfaces\Package $package, array $config) |
||
95 | { |
||
96 | if ($this->isValid($config, 'stability', 'string')) { |
||
97 | $package->setStability($config['stability']); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | protected function setPackageExtra(Interfaces\Package $package, array $config) |
||
102 | { |
||
103 | if ($this->isValid($config, 'extra', 'array')) { |
||
104 | $package->setExtra($config['extra']); |
||
|
|||
105 | } |
||
106 | } |
||
107 | |||
108 | protected function setPackageDescription(Interfaces\Package $package, array $config) |
||
109 | { |
||
110 | if ($this->isValid($config, 'description', 'string')) { |
||
111 | $package->setDescription($config['description']); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | protected function setPackageHomepage(Interfaces\Package $package, array $config) |
||
116 | { |
||
117 | if ($this->isValid($config, 'homepage', 'string')) { |
||
118 | $package->setHomepage($config['homepage']); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | protected function setPackageKeywords(Interfaces\Package $package, array $config) |
||
123 | { |
||
124 | if ($this->isValid($config, 'keywords', 'array')) { |
||
125 | $package->setKeywords($config['keywords']); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | protected function setPackageLicense(Interfaces\Package $package, array $config) |
||
130 | { |
||
131 | if (!empty($config['license'])) { |
||
132 | $package->setLicense(is_array($config['license']) ? $config['license'] : [$config['license']]); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | protected function setPackageAuthors(Interfaces\Package $package, array $config) |
||
140 | } |
||
141 | } |
||
142 | |||
143 | protected function setPackageSupport(Interfaces\Package $package, array $config) |
||
144 | { |
||
145 | if (isset($config['support'])) { |
||
146 | $package->setSupport($config['support']); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | protected function isValid($config, $key, $type = 'any') |
||
161 | } |
||
162 | |||
163 | protected function setPackageSource(Interfaces\Package $package, array $config) |
||
164 | { |
||
165 | if (!isset($config['source'])) { |
||
166 | return; |
||
167 | } |
||
168 | |||
169 | if (!isset($config['source']['type']) || !isset($config['source']['url']) || !isset($config['source']['reference'])) { |
||
170 | throw new UnexpectedValueException(sprintf( |
||
171 | "Package %s's source key should be specified as {\"type\": ..., \"url\": ..., \"reference\": ...},\n%s given.", |
||
172 | $config['name'], |
||
173 | json_encode($config['source']) |
||
174 | )); |
||
175 | } |
||
176 | $package->setSourceType($config['source']['type']); |
||
177 | $package->setSourceUrl($config['source']['url']); |
||
178 | $package->setSourceReference($config['source']['reference']); |
||
179 | if (isset($config['source']['mirrors'])) { |
||
180 | $package->setSourceMirrors($config['source']['mirrors']); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | protected function setPackageDist(Interfaces\Package $package, array $config) |
||
185 | { |
||
186 | if (!isset($config['dist'])) { |
||
187 | return; |
||
188 | } |
||
189 | |||
190 | if (!isset($config['dist']['type']) |
||
191 | || !isset($config['dist']['url'])) { |
||
192 | throw new UnexpectedValueException(sprintf( |
||
193 | "Package %s's dist key should be specified as " |
||
194 | . "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given.", |
||
195 | $config['name'], |
||
196 | json_encode($config['dist']) |
||
197 | )); |
||
198 | } |
||
199 | |||
200 | $package->setDistType($config['dist']['type']); |
||
201 | $package->setDistUrl($config['dist']['url']); |
||
202 | $package->setDistReference($config['dist']['reference'] ?? null); |
||
203 | $package->setDistSha1Checksum($config['dist']['shasum'] ?? null); |
||
204 | if (isset($config['dist']['mirrors'])) { |
||
205 | $package->setDistMirrors($config['dist']['mirrors']); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | protected function setPackageReleaseDate(Interfaces\Package $package, array $config) |
||
221 | // don't crash if time is incorrect |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
||
227 |