Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
24 | class RuleChecker |
||
25 | { |
||
26 | |||
27 | use StaticCacheTrait; |
||
28 | |||
29 | /** |
||
30 | * List of rules to check. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $_rules = []; |
||
35 | |||
36 | /** |
||
37 | * List of rules marked as FAIL. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $_fail = []; |
||
42 | |||
43 | /** |
||
44 | * List of rules marked as PASS. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $_pass = []; |
||
49 | |||
50 | /** |
||
51 | * Whether rules were checked using check() or not. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $_checked = false; |
||
56 | |||
57 | /** |
||
58 | * Constructor. |
||
59 | * |
||
60 | * ### Basic usage: |
||
61 | * |
||
62 | * ```php |
||
63 | * $rules = [ |
||
64 | * 'php' => '5.3.*', |
||
65 | * 'quickapps/cms' => '2.*', |
||
66 | * ]; |
||
67 | * |
||
68 | * $checker = new RuleChecker($rules); |
||
69 | * |
||
70 | * if ($checker->check()) { |
||
71 | * // all OK |
||
72 | * } else { |
||
73 | * // ERROR, get failing rules: |
||
74 | * $checker->fail(); |
||
75 | * } |
||
76 | * ``` |
||
77 | * |
||
78 | * ### Without invoking check(): |
||
79 | * |
||
80 | * You can also use `pass()` or `fail()` methods before invoking `check()` as |
||
81 | * in the example below. |
||
82 | * |
||
83 | * ```php |
||
84 | * $checker = new RuleChecker($rules); |
||
85 | * $pass = $checker->pass(); |
||
86 | * $fail = $checker->fail(); |
||
87 | * ``` |
||
88 | * |
||
89 | * ### Providing packages as objects: |
||
90 | * |
||
91 | * ```php |
||
92 | * $rules = [ |
||
93 | * new MyPackage('vendor/package', '/path/to/package') => '1.3.*', |
||
94 | * 'quickapps/cms' => '2.*', |
||
95 | * ]; |
||
96 | * } |
||
97 | * ``` |
||
98 | * |
||
99 | * @param array $rules A list of rules given as `package` => `constraints`, |
||
100 | * where the left hand side (package) can be either a string representing |
||
101 | * a package (as "vendor/package") or an object extending |
||
102 | * \CMS\Core\Package\BasePackage |
||
103 | */ |
||
104 | public function __construct(array $rules) |
||
110 | |||
111 | /** |
||
112 | * Checks all the rules of this class. |
||
113 | * |
||
114 | * @return bool True if all rules are meet |
||
115 | */ |
||
116 | public function check() |
||
138 | |||
139 | /** |
||
140 | * Gets all rules marked as PASS. |
||
141 | * |
||
142 | * @param bool $asString True will returns a comma separated string of rules |
||
143 | * @return array|string |
||
144 | */ |
||
145 | View Code Duplication | public function pass($asString = false) |
|
162 | |||
163 | /** |
||
164 | * Gets all rules marked as FAIL. |
||
165 | * |
||
166 | * @param bool $asString True will returns a comma separated string of rules |
||
167 | * @return array|string |
||
168 | */ |
||
169 | View Code Duplication | public function fail($asString = false) |
|
186 | |||
187 | /** |
||
188 | * Marks a rule as PASS. |
||
189 | * |
||
190 | * @param \CMS\Core\Package\Rule\Rule $rule The rule to mark |
||
191 | * @return void |
||
192 | */ |
||
193 | protected function _pass(Rule $rule) |
||
197 | |||
198 | /** |
||
199 | * Marks a rule as FAIL. |
||
200 | * |
||
201 | * @param \CMS\Core\Package\Rule\Rule $rule The rule to mark |
||
202 | * @return void |
||
203 | */ |
||
204 | protected function _fail(Rule $rule) |
||
208 | } |
||
209 |