Complex classes like Package 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Package, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class Package |
||
48 | { |
||
49 | /** |
||
50 | * @var Composer |
||
51 | */ |
||
52 | protected $composer; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected static $forEachRefHeadSupported = true; |
||
58 | |||
59 | /** |
||
60 | * Package constructor. |
||
61 | * |
||
62 | * @param Composer $composer The parent object |
||
63 | * @param string|object $composerJson The composer json |
||
64 | * @param bool $isRoot Whether package is root |
||
65 | */ |
||
66 | public function __construct(Composer $composer, $composerJson, $isRoot = false) |
||
67 | { |
||
68 | $this->composer = $composer; |
||
69 | |||
70 | if (is_string($composerJson)) { |
||
71 | $path = realpath($composerJson); |
||
72 | if (!$path) { |
||
73 | throw new Exception('Could not find ' . $composerJson); |
||
74 | } |
||
75 | $this->path = dirname($path); |
||
76 | $composerJson = json_decode(file_get_contents($path)); |
||
77 | if (!is_object($composerJson)) { |
||
78 | throw new Exception('Could not load ' . $path); |
||
79 | } |
||
80 | } |
||
81 | foreach (get_object_vars($composerJson) as $key => $value) { |
||
82 | $this->$key = $value; |
||
83 | } |
||
84 | |||
85 | $this->isRoot = $isRoot; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Load lazy properties |
||
90 | * |
||
91 | * @param string $name The property name |
||
92 | * |
||
93 | * @return mixed |
||
94 | */ |
||
95 | public function __get($name) |
||
96 | { |
||
97 | switch ($name) { |
||
98 | case 'git': |
||
99 | $gitDir = $this->path . '/.git'; |
||
100 | $this->git = file_exists($gitDir) && is_dir($gitDir); |
||
101 | break; |
||
102 | case 'branches': |
||
103 | case 'upstreams': |
||
104 | case 'branch': |
||
105 | $this->loadGitInformation(); |
||
106 | break; |
||
107 | case 'tag': |
||
108 | $this->loadTag(); |
||
109 | break; |
||
110 | case 'remote': |
||
111 | $this->loadRemote(); |
||
112 | break; |
||
113 | case 'requires': |
||
114 | $this->loadRequires(); |
||
115 | break; |
||
116 | default: |
||
117 | throw new Exception('Invalid property ' . $name); |
||
118 | |||
119 | } |
||
120 | return $this->$name; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Mark lazy properties as present |
||
125 | * |
||
126 | * @param string $name The name |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function __isset($name) |
||
134 | |||
135 | /** |
||
136 | * Load the requires - removes inline aliases |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | protected function loadRequires() |
||
153 | |||
154 | /** |
||
155 | * Reload requires from composer.json |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function reloadRequires() |
||
172 | |||
173 | /** |
||
174 | * Get the remote |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | protected function loadRemote() |
||
199 | |||
200 | /** |
||
201 | * Load 'branches', 'upstreams', 'branch', 'tag', 'git' |
||
202 | * |
||
203 | * @throws Exception\ProcessFailedException |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | protected function loadGitInformation() |
||
208 | { |
||
209 | $this->branches = array(); |
||
210 | $this->upstreams = array(); |
||
211 | $this->branch = null; |
||
212 | if ($this->git) { |
||
213 | $this->composer->git('fetch', $this->path, array('p' => true, 'origin')); |
||
214 | try { |
||
215 | $format = (self::$forEachRefHeadSupported ? '%(HEAD)' : '') . '|%(refname:short)|%(upstream:short)'; |
||
216 | $gitBr = $this->composer->git('for-each-ref', $this->path, ['format' => $format, 'refs/heads/', 'refs/remotes/origin']); |
||
217 | } catch (Exception\ProcessFailedException $e) { |
||
218 | if (trim($e->getProcess()->getErrorOutput()) === 'fatal: unknown field name: HEAD') { |
||
219 | self::$forEachRefHeadSupported = false; |
||
220 | $this->loadGitInformation(); |
||
221 | return; |
||
222 | } else { |
||
223 | throw $e; |
||
224 | } |
||
225 | } |
||
226 | if (!self::$forEachRefHeadSupported) { |
||
227 | $this->branch = $this->composer->git('rev-parse', $this->path, ['abbrev-ref' => true, 'HEAD']) ?: null; |
||
228 | if ($this->branch === 'HEAD') { |
||
229 | $this->branch = null; |
||
230 | } |
||
231 | } |
||
232 | foreach (explode("\n", trim($gitBr)) as $line) { |
||
233 | list($head, $branch, $upstream) = explode('|', $line); |
||
234 | if ($branch === 'origin/HEAD') { |
||
235 | continue; |
||
236 | } |
||
237 | $this->branches[] = $branch; |
||
238 | if ($head === '*') { |
||
239 | $this->branch = $branch; |
||
240 | } |
||
241 | if ($upstream) { |
||
242 | $this->upstreams[$branch] = $upstream; |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Load the currently checked out tag |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | protected function loadTag() |
||
261 | } |
||
262 | |||
263 | ?> |
||
264 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.