Total Complexity | 45 |
Total Lines | 161 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Loader 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.
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 Loader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Loader |
||
9 | { |
||
10 | public $errors = []; |
||
11 | //options |
||
12 | public const EXCLUDE_DIRECTIVES = 0001;//DONT include_directive |
||
13 | public const IGNORE_COMMENTS = 0010;//DONT include_comments |
||
14 | public const EXCEPTIONS_PARSING = 0100;//THROW Exception on parsing Errors |
||
15 | public const NO_OBJECT_FOR_DATE = 1000;//DONT import date strings as dateTime Object |
||
16 | // |
||
17 | private $content; |
||
18 | private $filePath; |
||
19 | private $debug = 0;//TODO: determine levels |
||
20 | private $options = 0; |
||
21 | //Exceptions |
||
22 | const INVALID_VALUE = self::class.": at line %d"; |
||
23 | const EXCEPTION_NO_FILE = self::class.": file '%s' does not exists (or path is incorrect?)"; |
||
24 | const EXCEPTION_READ_ERROR = self::class.": file '%s' failed to be loaded (permission denied ?)"; |
||
25 | const EXCEPTION_LINE_SPLIT = self::class.": content is not a string(maybe a file error?)"; |
||
26 | |||
27 | public function __construct($absolutePath = null, $options = null, $debug = 0) |
||
28 | { |
||
29 | $this->debug = is_int($debug) ? min($debug, 3) : 1; |
||
30 | if (!is_null($options)) { |
||
31 | $this->options = $options; |
||
32 | } |
||
33 | if (!is_null($absolutePath)) { |
||
34 | $this->load($absolutePath); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | public function load(String $absolutePath):Loader |
||
39 | { |
||
40 | $this->debug && var_dump($absolutePath); |
||
1 ignored issue
–
show
|
|||
41 | $this->filePath = $absolutePath; |
||
42 | if (!file_exists($absolutePath)) { |
||
43 | throw new \Exception(sprintf(self::EXCEPTION_NO_FILE, $absolutePath)); |
||
44 | } |
||
45 | $adle = "auto_detect_line_endings"; |
||
46 | $prevADLE = ini_get($adle); |
||
47 | !$prevADLE && ini_set($adle, "true"); |
||
48 | $content = file($absolutePath, FILE_IGNORE_NEW_LINES); |
||
49 | !$prevADLE && ini_set($adle, "false"); |
||
50 | if (is_bool($content)) { |
||
51 | throw new \Exception(sprintf(self::EXCEPTION_READ_ERROR, $absolutePath)); |
||
52 | } |
||
53 | $this->content = $content; |
||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Parse Yaml lines into an hierarchy of Node |
||
59 | * |
||
60 | * @param string $strContent The Yaml string or null to parse loaded content |
||
3 ignored issues
–
show
|
|||
61 | * @throws \Exception if content is not available as $strContent or as $this->content (from file) |
||
1 ignored issue
–
show
|
|||
62 | * @throws \ParseError if any error during parsing or building |
||
1 ignored issue
–
show
|
|||
63 | * |
||
64 | * @return array|YamlObject the hierarchy built an array of YamlObject or just YamlObject |
||
1 ignored issue
–
show
|
|||
65 | */ |
||
66 | public function parse($strContent = null) |
||
120 | } |
||
121 | } |
||
122 | |||
123 | private function onSpecialType(&$n, &$parent, &$previous, &$emptyLines):bool |
||
140 | } |
||
141 | } |
||
142 | |||
143 | private function onDeepestType(&$n, &$parent, &$previous, $lineString):bool |
||
171 |