Complex classes like Highlighter 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 Highlighter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Highlighter implements HighlighterContract |
||
26 | { |
||
27 | /** |
||
28 | * Holds the theme. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | private $theme = [ |
||
33 | self::TOKEN_STRING => ['light_gray'], |
||
34 | self::TOKEN_COMMENT => ['dark_gray', 'italic'], |
||
35 | self::TOKEN_KEYWORD => ['magenta', 'bold'], |
||
36 | self::TOKEN_DEFAULT => ['default', 'bold'], |
||
37 | self::TOKEN_HTML => ['blue', 'bold'], |
||
38 | self::ACTUAL_LINE_MARK => ['red', 'bold'], |
||
39 | self::LINE_NUMBER => ['dark_gray'], |
||
40 | ]; |
||
41 | |||
42 | const TOKEN_DEFAULT = 'token_default'; |
||
43 | const TOKEN_COMMENT = 'token_comment'; |
||
44 | const TOKEN_STRING = 'token_string'; |
||
45 | const TOKEN_HTML = 'token_html'; |
||
46 | const TOKEN_KEYWORD = 'token_keyword'; |
||
47 | |||
48 | const ACTUAL_LINE_MARK = 'actual_line_mark'; |
||
49 | const LINE_NUMBER = 'line_number'; |
||
50 | |||
51 | /** @var ConsoleColor */ |
||
52 | private $color; |
||
53 | |||
54 | /** @var array */ |
||
55 | private $defaultTheme = [ |
||
56 | self::TOKEN_STRING => 'red', |
||
57 | self::TOKEN_COMMENT => 'yellow', |
||
58 | self::TOKEN_KEYWORD => 'green', |
||
59 | self::TOKEN_DEFAULT => 'default', |
||
60 | self::TOKEN_HTML => 'cyan', |
||
61 | |||
62 | self::ACTUAL_LINE_MARK => 'red', |
||
63 | self::LINE_NUMBER => 'dark_gray', |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * Creates an instance of the Highlighter. |
||
68 | */ |
||
69 | 13 | public function __construct(ConsoleColor $color = null) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 4 | public function highlight(string $content, int $line): string |
|
91 | |||
92 | /** |
||
93 | * @param string $source |
||
94 | * @param int $lineNumber |
||
95 | * @param int $linesBefore |
||
96 | * @param int $linesAfter |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 4 | public function getCodeSnippet($source, $lineNumber, $linesBefore = 2, $linesAfter = 2) |
|
113 | |||
114 | /** |
||
115 | * @param string $source |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | 4 | private function getHighlightedLines($source) |
|
126 | |||
127 | /** |
||
128 | * @param string $source |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 4 | private function tokenize($source) |
|
206 | |||
207 | /** |
||
208 | * @return array |
||
209 | */ |
||
210 | 4 | private function splitToLines(array $tokens) |
|
234 | |||
235 | /** |
||
236 | * @return array |
||
237 | */ |
||
238 | 4 | private function colorLines(array $tokenLines) |
|
256 | |||
257 | /** |
||
258 | * @param int|null $markLine |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | 4 | private function lineNumbers(array $lines, $markLine = null) |
|
279 | } |
||
280 |
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: