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:
Complex classes like PhpSecInfo 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 PhpSecInfo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
95 | class PhpSecInfo |
||
|
|||
96 | { |
||
97 | |||
98 | /** |
||
99 | * An array of tests to run |
||
100 | * |
||
101 | * @var array PhpSecInfo_Test |
||
102 | */ |
||
103 | var $tests_to_run = array(); |
||
104 | |||
105 | |||
106 | /** |
||
107 | * An array of results. Each result is an associative array: |
||
108 | * <code> |
||
109 | * $result['result'] = PHPSECINFO_TEST_RESULT_NOTICE; |
||
110 | * $result['message'] = "a string describing the test results and what they mean"; |
||
111 | * </code> |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | var $test_results = array(); |
||
116 | |||
117 | |||
118 | /** |
||
119 | * An array of tests that were not run |
||
120 | * |
||
121 | * <code> |
||
122 | * $result['result'] = PHPSECINFO_TEST_RESULT_NOTRUN; |
||
123 | * $result['message'] = "a string explaining why the test was not run"; |
||
124 | * </code> |
||
125 | * |
||
126 | * @var array |
||
127 | */ |
||
128 | var $tests_not_run = array(); |
||
129 | |||
130 | |||
131 | /** |
||
132 | * The language code used. Defaults to PHPSECINFO_LANG_DEFAULT, which |
||
133 | * is 'en' |
||
134 | * |
||
135 | * @var string |
||
136 | * @see PHPSECINFO_LANG_DEFAULT |
||
137 | */ |
||
138 | var $language = PHPSECINFO_LANG_DEFAULT; |
||
139 | |||
140 | |||
141 | /** |
||
142 | * An array of integers recording the number of test results in each category. Categories can include |
||
143 | * some or all of the PHPSECINFO_TEST_* constants. Constants are the keys, # of results are the values. |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | var $result_counts = array(); |
||
148 | |||
149 | |||
150 | /** |
||
151 | * The number of tests that have been run |
||
152 | * |
||
153 | * @var integer |
||
154 | */ |
||
155 | var $num_tests_run = 0; |
||
156 | |||
157 | |||
158 | /** |
||
159 | * The base directory for phpsecinfo. Set within the constructor. Paths are resolved from this. |
||
160 | * @var string |
||
161 | */ |
||
162 | var $_base_dir; |
||
163 | |||
164 | |||
165 | /** |
||
166 | * The directory PHPSecInfo will look for views. It defaults to the value |
||
167 | * in PHPSECINFO_VIEW_DIR_DEFAULT, but can be changed with the setViewDirectory() |
||
168 | * method. |
||
169 | * |
||
170 | * @var string |
||
171 | */ |
||
172 | var $_view_directory; |
||
173 | |||
174 | |||
175 | /** |
||
176 | * The output format, used to load the proper view |
||
177 | * |
||
178 | * @var string |
||
179 | **/ |
||
180 | var $_format; |
||
181 | |||
182 | /** |
||
183 | * Constructor |
||
184 | * |
||
185 | * @return PhpSecInfo |
||
186 | */ |
||
187 | function __construct($opts = null) { |
||
188 | |||
189 | $this->_base_dir = dirname(__FILE__); |
||
190 | |||
191 | if ($opts) { |
||
192 | if (isset($opts['view_directory'])) { |
||
193 | $this->setViewDirectory($opts['view_directory']); |
||
194 | } else { |
||
195 | $this->setViewDirectory(dirname(__FILE__).DIRECTORY_SEPARATOR . PHPSECINFO_VIEW_DIR_DEFAULT); |
||
196 | } |
||
197 | |||
198 | if (isset($opts['format'])) { |
||
199 | $this->setFormat($opts['format']); |
||
200 | } else { |
||
201 | if (!strcasecmp(PHP_SAPI, 'cli')) { |
||
202 | $this->setFormat('Cli'); |
||
203 | } else { |
||
204 | $this->setFormat(PHPSECINFO_FORMAT_DEFAULT); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | } else { /* Use defaults */ |
||
209 | $this->setViewDirectory(dirname(__FILE__).DIRECTORY_SEPARATOR . PHPSECINFO_VIEW_DIR_DEFAULT); |
||
210 | if (!strcasecmp(PHP_SAPI, 'cli')) { |
||
211 | $this->setFormat('Cli'); |
||
212 | } else { |
||
213 | $this->setFormat(PHPSECINFO_FORMAT_DEFAULT); |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * recurses through the Test subdir and includes classes in each test group subdir, |
||
221 | * then builds an array of classnames for the tests that will be run |
||
222 | * |
||
223 | */ |
||
224 | function loadTests() { |
||
253 | |||
254 | |||
255 | /** |
||
256 | * This runs the tests in the tests_to_run array and |
||
257 | * places returned data in the following arrays/scalars: |
||
258 | * - $this->test_results |
||
259 | * - $this->result_counts |
||
260 | * - $this->num_tests_run |
||
261 | * - $this->tests_not_run; |
||
262 | * |
||
263 | */ |
||
264 | function runTests() { |
||
307 | |||
308 | |||
309 | /** |
||
310 | * This is the main output method. The look and feel mimics phpinfo() |
||
311 | * |
||
312 | */ |
||
313 | function renderOutput($page_title="Security Information About PHP") { |
||
323 | |||
324 | |||
325 | /** |
||
326 | * This is a helper method that makes it easy to output tables of test results |
||
327 | * for a given test group |
||
328 | * |
||
329 | * @param string $group_name |
||
330 | * @param array $group_results |
||
331 | */ |
||
332 | function _outputRenderTable($group_name, $group_results) { |
||
345 | |||
346 | |||
347 | |||
348 | /** |
||
349 | * This outputs a table containing a summary of the test results (counts and % in each result type) |
||
350 | * |
||
351 | * @see PHPSecInfo::_outputRenderTable() |
||
352 | * @see PHPSecInfo::_outputGetResultTypeFromCode() |
||
353 | */ |
||
354 | function _outputRenderStatsTable() { |
||
369 | |||
370 | |||
371 | |||
372 | /** |
||
373 | * This outputs a table containing a summary or test that were not executed, and the reasons why they were skipped |
||
374 | * |
||
375 | * @see PHPSecInfo::_outputRenderTable() |
||
376 | */ |
||
377 | function _outputRenderNotRunTable() { |
||
382 | |||
383 | |||
384 | |||
385 | |||
386 | /** |
||
387 | * This is a helper function that returns a CSS class corresponding to |
||
388 | * the result code the test returned. This allows us to color-code |
||
389 | * results |
||
390 | * |
||
391 | * @param integer $code |
||
392 | * @return string |
||
393 | */ |
||
394 | View Code Duplication | function _outputGetCssClassFromResult($code) { |
|
423 | |||
424 | |||
425 | |||
426 | /** |
||
427 | * This is a helper function that returns a label string corresponding to |
||
428 | * the result code the test returned. This is mainly used for the Test |
||
429 | * Results Summary table. |
||
430 | * |
||
431 | * @see PHPSecInfo::_outputRenderStatsTable() |
||
432 | * @param integer $code |
||
433 | * @return string |
||
434 | */ |
||
435 | View Code Duplication | function _outputGetResultTypeFromCode($code) { |
|
464 | |||
465 | |||
466 | /** |
||
467 | * Loads and runs all the tests |
||
468 | * |
||
469 | * As loading, then running, is a pretty common process, this saves a extra method call |
||
470 | * |
||
471 | * @since 0.1.1 |
||
472 | * |
||
473 | */ |
||
474 | function loadAndRun() { |
||
478 | |||
479 | |||
480 | /** |
||
481 | * returns an associative array of test data. Four keys are set: |
||
482 | * - test_results (array) |
||
483 | * - tests_not_run (array) |
||
484 | * - result_counts (array) |
||
485 | * - num_tests_run (integer) |
||
486 | * |
||
487 | * note that this must be called after tests are loaded and run |
||
488 | * |
||
489 | * @since 0.1.1 |
||
490 | * @return array |
||
491 | */ |
||
492 | function getResultsAsArray() { |
||
502 | |||
503 | |||
504 | |||
505 | /** |
||
506 | * returns the standard output as a string instead of echoing it to the browser |
||
507 | * |
||
508 | * note that this must be called after tests are loaded and run |
||
509 | * |
||
510 | * @since 0.1.1 |
||
511 | * |
||
512 | * @return string |
||
513 | */ |
||
514 | function getOutput() { |
||
520 | |||
521 | |||
522 | /** |
||
523 | * A very, very simple "view" system |
||
524 | * |
||
525 | */ |
||
526 | function loadView($view_name, $data=null) { |
||
543 | |||
544 | |||
545 | /** |
||
546 | * Returns the current view directory |
||
547 | * |
||
548 | * @return string |
||
549 | */ |
||
550 | function getViewDirectory() { |
||
553 | |||
554 | |||
555 | /** |
||
556 | * Sets the directory that PHPSecInfo will look in for views |
||
557 | * |
||
558 | * @param string $newdir |
||
559 | */ |
||
560 | function setViewDirectory($newdir) { |
||
563 | |||
564 | |||
565 | |||
566 | |||
567 | function getFormat() { |
||
570 | |||
571 | |||
572 | function setFormat($format) { |
||
575 | |||
576 | } |
||
577 | |||
592 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.