Complex classes like Block 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 Block, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Block |
||
| 11 | { |
||
| 12 | protected $calls = array(); |
||
| 13 | protected $skipEol = false; |
||
| 14 | protected $inParagraph = false; |
||
| 15 | |||
| 16 | // Blocks these should not be inside paragraphs |
||
| 17 | protected $blockOpen = array( |
||
| 18 | 'header', |
||
| 19 | 'listu_open','listo_open','listitem_open','listcontent_open', |
||
| 20 | 'table_open','tablerow_open','tablecell_open','tableheader_open','tablethead_open', |
||
| 21 | 'quote_open', |
||
| 22 | 'code','file','hr','preformatted','rss', |
||
| 23 | 'htmlblock','phpblock', |
||
| 24 | 'footnote_open', |
||
| 25 | ); |
||
| 26 | |||
| 27 | protected $blockClose = array( |
||
| 28 | 'header', |
||
| 29 | 'listu_close','listo_close','listitem_close','listcontent_close', |
||
| 30 | 'table_close','tablerow_close','tablecell_close','tableheader_close','tablethead_close', |
||
| 31 | 'quote_close', |
||
| 32 | 'code','file','hr','preformatted','rss', |
||
| 33 | 'htmlblock','phpblock', |
||
| 34 | 'footnote_close', |
||
| 35 | ); |
||
| 36 | |||
| 37 | // Stacks can contain paragraphs |
||
| 38 | protected $stackOpen = array( |
||
| 39 | 'section_open', |
||
| 40 | ); |
||
| 41 | |||
| 42 | protected $stackClose = array( |
||
| 43 | 'section_close', |
||
| 44 | ); |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor. Adds loaded syntax plugins to the block and stack |
||
| 49 | * arrays |
||
| 50 | * |
||
| 51 | * @author Andreas Gohr <[email protected]> |
||
| 52 | */ |
||
| 53 | public function __construct() |
||
| 69 | |||
| 70 | protected function openParagraph($pos) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Close a paragraph if needed |
||
| 80 | * |
||
| 81 | * This function makes sure there are no empty paragraphs on the stack |
||
| 82 | * |
||
| 83 | * @author Andreas Gohr <[email protected]> |
||
| 84 | * |
||
| 85 | * @param string|integer $pos |
||
| 86 | */ |
||
| 87 | protected function closeParagraph($pos) |
||
| 119 | |||
| 120 | protected function addCall($call) |
||
| 129 | |||
| 130 | // simple version of addCall, without checking cdata |
||
| 131 | protected function storeCall($call) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Processes the whole instruction stack to open and close paragraphs |
||
| 138 | * |
||
| 139 | * @author Harry Fuecks <[email protected]> |
||
| 140 | * @author Andreas Gohr <[email protected]> |
||
| 141 | * |
||
| 142 | * @param array $calls |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function process($calls) |
||
| 211 | } |
||
| 212 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: