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 SectionProfiler 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 SectionProfiler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class SectionProfiler { |
||
| 32 | /** @var array Map of (mem,real,cpu) */ |
||
| 33 | protected $start; |
||
| 34 | /** @var array Map of (mem,real,cpu) */ |
||
| 35 | protected $end; |
||
| 36 | /** @var array List of resolved profile calls with start/end data */ |
||
| 37 | protected $stack = []; |
||
| 38 | /** @var array Queue of open profile calls with start data */ |
||
| 39 | protected $workStack = []; |
||
| 40 | |||
| 41 | /** @var array Map of (function name => aggregate data array) */ |
||
| 42 | protected $collated = []; |
||
| 43 | /** @var bool */ |
||
| 44 | protected $collateDone = false; |
||
| 45 | |||
| 46 | /** @var bool Whether to collect the full stack trace or just aggregates */ |
||
| 47 | protected $collateOnly = true; |
||
| 48 | /** @var array Cache of a standard broken collation entry */ |
||
| 49 | protected $errorEntry; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param array $params |
||
| 53 | */ |
||
| 54 | public function __construct( array $params = [] ) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $section |
||
| 61 | * @return SectionProfileCallback |
||
| 62 | */ |
||
| 63 | public function scopedProfileIn( $section ) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param ScopedCallback $section |
||
| 71 | */ |
||
| 72 | public function scopedProfileOut( ScopedCallback &$section ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the aggregated inclusive profiling data for each method |
||
| 78 | * |
||
| 79 | * The percent time for each time is based on the current "total" time |
||
| 80 | * used is based on all methods so far. This method can therefore be |
||
| 81 | * called several times in between several profiling calls without the |
||
| 82 | * delays in usage of the profiler skewing the results. A "-total" entry |
||
| 83 | * is always included in the results. |
||
| 84 | * |
||
| 85 | * @return array List of method entries arrays, each having: |
||
| 86 | * - name : method name |
||
| 87 | * - calls : the number of invoking calls |
||
| 88 | * - real : real time elapsed (ms) |
||
| 89 | * - %real : percent real time |
||
| 90 | * - cpu : real time elapsed (ms) |
||
| 91 | * - %cpu : percent real time |
||
| 92 | * - memory : memory used (bytes) |
||
| 93 | * - %memory : percent memory used |
||
| 94 | * - min_real : min real time in a call (ms) |
||
| 95 | * - max_real : max real time in a call (ms) |
||
| 96 | */ |
||
| 97 | public function getFunctionStats() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Clear all of the profiling data for another run |
||
| 138 | */ |
||
| 139 | public function reset() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return array Initial collation entry |
||
| 150 | */ |
||
| 151 | protected function getZeroEntry() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return array Initial collation entry for errors |
||
| 164 | */ |
||
| 165 | protected function getErrorEntry() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Update the collation entry for a given method name |
||
| 173 | * |
||
| 174 | * @param string $name |
||
| 175 | * @param float $elapsedCpu |
||
| 176 | * @param float $elapsedReal |
||
| 177 | * @param int $memChange |
||
| 178 | */ |
||
| 179 | protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * This method should not be called outside SectionProfiler |
||
| 195 | * |
||
| 196 | * @param string $functionname |
||
| 197 | */ |
||
| 198 | public function profileInInternal( $functionname ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * This method should not be called outside SectionProfiler |
||
| 223 | * |
||
| 224 | * @param string $functionname |
||
| 225 | */ |
||
| 226 | public function profileOutInternal( $functionname ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns a tree of function calls with their real times |
||
| 275 | * @return string |
||
| 276 | * @throws Exception |
||
| 277 | */ |
||
| 278 | public function getCallTreeReport() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Recursive function the format the current profiling array into a tree |
||
| 289 | * |
||
| 290 | * @param array $stack Profiling array |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | protected function remapCallTree( array $stack ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Callback to get a formatted line for the call tree |
||
| 330 | * @param array $entry |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | protected function getCallTreeLine( $entry ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Populate collated data |
||
| 346 | */ |
||
| 347 | protected function collateData() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Dummy calls to calculate profiling overhead |
||
| 420 | * |
||
| 421 | * @param int $profileCount |
||
| 422 | */ |
||
| 423 | protected function calculateOverhead( $profileCount ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Counts the number of profiled function calls sitting under |
||
| 434 | * the given point in the call graph. Not the most efficient algo. |
||
| 435 | * |
||
| 436 | * @param array $stack |
||
| 437 | * @param int $start |
||
| 438 | * @return int |
||
| 439 | */ |
||
| 440 | protected function calltreeCount( $stack, $start ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get the initial time of the request, based on getrusage() |
||
| 451 | * |
||
| 452 | * @param string|bool $metric Metric to use, with the following possibilities: |
||
| 453 | * - user: User CPU time (without system calls) |
||
| 454 | * - cpu: Total CPU time (user and system calls) |
||
| 455 | * - wall (or any other string): elapsed time |
||
| 456 | * - false (default): will fall back to default metric |
||
| 457 | * @return float |
||
| 458 | */ |
||
| 459 | protected function getTime( $metric = 'wall' ) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Add an entry in the debug log file |
||
| 479 | * |
||
| 480 | * @param string $s String to output |
||
| 481 | */ |
||
| 482 | protected function debug( $s ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Add an entry in the debug log group |
||
| 490 | * |
||
| 491 | * @param string $group Group to send the message to |
||
| 492 | * @param string $s String to output |
||
| 493 | */ |
||
| 494 | protected function debugGroup( $group, $s ) { |
||
| 499 | } |
||
| 500 | |||
| 526 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: