Complex classes like CommitsHandler 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 CommitsHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class CommitsHandler extends BaseHandler |
||
20 | { |
||
21 | public $type = 'commits'; |
||
22 | |||
23 | protected $_tag; |
||
24 | protected $_note; |
||
25 | protected $_hash; |
||
26 | protected $_history = []; |
||
27 | protected $_commits = []; |
||
28 | |||
29 | public function getTag() |
||
33 | |||
34 | public function getNote() |
||
38 | |||
39 | public function getHash() |
||
43 | |||
44 | public function setTag($tag) |
||
45 | { |
||
46 | $this->_tag = $tag; |
||
47 | $this->_note = ''; |
||
48 | $this->_hash = ''; |
||
49 | } |
||
50 | |||
51 | public function setNote($note) |
||
52 | { |
||
53 | $this->_note = $note; |
||
54 | $this->_hash = ''; |
||
55 | } |
||
56 | |||
57 | public function setHash($hash) |
||
61 | |||
62 | public function addTag($tag, $label = null) |
||
63 | { |
||
64 | $this->tag = $tag; |
||
|
|||
65 | $ref = &$this->_history[$tag]['tag']; |
||
66 | $ref = $label ?: $ref ?: $tag; |
||
67 | } |
||
68 | |||
69 | public function addNote($note, $label = null) |
||
70 | { |
||
71 | $this->note = $note; |
||
72 | $ref = &$this->_history[$this->tag][$note]['note']; |
||
73 | $ref = $label ?: $ref ?: $note; |
||
74 | } |
||
75 | |||
76 | public function addHash($hash, $label) |
||
77 | { |
||
78 | $this->_hash = $hash; |
||
79 | $this->_commits[(string) $hash] = $label; |
||
80 | } |
||
81 | |||
82 | public function hasCommit($hash) |
||
86 | |||
87 | public function parsePath($path, $minimal = null) |
||
88 | { |
||
89 | $this->tag = static::getVcs()->lastTag; |
||
90 | $this->_history = [ |
||
91 | $this->tag => [], |
||
92 | ]; |
||
93 | $lines = is_file($path) ? $this->readArray($path) : []; |
||
94 | foreach ($lines as $str) { |
||
95 | $str = rtrim($str); |
||
96 | ++$no; |
||
97 | if (!$str || $no < 3) { |
||
98 | continue; |
||
99 | } |
||
100 | if (preg_match('/^# /', $str)) { |
||
101 | continue; |
||
102 | }; |
||
103 | if (preg_match('/^## (.*)$/', $str, $m)) { |
||
104 | $label = $m[1]; |
||
105 | foreach ([static::getVcs()->lastTag, static::getVcs()->initTag] as $z) { |
||
106 | if (stripos($label, $z) !== false) { |
||
107 | $this->addTag($z, $label); |
||
108 | continue 2; |
||
109 | } |
||
110 | } |
||
111 | preg_match('/^(\S+)\s*(.*)$/', $label, $m); |
||
112 | $this->addTag($m[1], $label); |
||
113 | continue; |
||
114 | } |
||
115 | if (preg_match('/^- (.*)$/', $str, $m)) { |
||
116 | $this->addNote($m[1]); |
||
117 | continue; |
||
118 | } |
||
119 | if (preg_match('/^\s+- ([0-9a-fA-F]{7})/', $str, $m)) { |
||
120 | $this->addHash($m[1], $str); |
||
121 | } |
||
122 | $this->_history[$this->tag][$this->note][$this->hash][] = $str; |
||
123 | } |
||
124 | |||
125 | return $this->_history; |
||
126 | } |
||
127 | |||
128 | public function addHistory($commit, $front = false) |
||
129 | { |
||
130 | $tag = $commit['tag']; |
||
131 | $note = $commit['note']; |
||
132 | $hash = $commit['hash']; |
||
133 | $render = static::renderCommit($commit); |
||
134 | $hashes = &$this->_history[$tag][$note]; |
||
135 | $hashes = (array) $hashes; |
||
136 | if ($front) { |
||
137 | $hashes = [$hash => [$render]] + $hashes; |
||
138 | } else { |
||
139 | $hashes[$hash][] = $render; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | public function hasHistory($tag) |
||
147 | |||
148 | public function setHistory($value) |
||
149 | { |
||
150 | $this->_history = $value; |
||
151 | } |
||
152 | |||
153 | public function getHistory() |
||
154 | { |
||
155 | return $this->_history; |
||
157 | |||
158 | public function render($data) |
||
201 | |||
202 | public static function arrayPop(&$array, $key) |
||
209 | |||
210 | public static function renderNote($note) |
||
214 | |||
215 | public static function renderLines(array $lines) |
||
221 | |||
222 | public static function renderCommit($commit) |
||
226 | |||
227 | public static function skipCommit($commit) |
||
252 | |||
253 | public static function renderTag($tag) |
||
261 | |||
262 | public static function renderDate($date) |
||
266 | |||
267 | public static function renderHeader($string) |
||
273 | |||
274 | public static function getVcs() |
||
278 | } |
||
279 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.