| Total Complexity | 58 |
| Total Lines | 209 |
| 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 |
||
| 15 | final class Loader |
||
| 16 | { |
||
| 17 | //public |
||
| 18 | /* @var null|string */ |
||
| 19 | public static $error; |
||
| 20 | public const IGNORE_DIRECTIVES = 1;//DONT include_directive |
||
| 21 | public const IGNORE_COMMENTS = 2;//DONT include_comments |
||
| 22 | public const NO_PARSING_EXCEPTIONS = 4;//DONT throw Exception on parsing errors |
||
| 23 | public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
||
| 24 | |||
| 25 | //privates |
||
| 26 | /* @var null|false|array */ |
||
| 27 | private $content; |
||
| 28 | /* @var null|string */ |
||
| 29 | private $filePath; |
||
| 30 | /* @var integer */ |
||
| 31 | private $debug = 0;///TODO: determine levels |
||
| 32 | /* @var integer */ |
||
| 33 | private $options = 0; |
||
| 34 | //Exceptions messages |
||
| 35 | private const INVALID_VALUE = self::class.": at line %d"; |
||
| 36 | private const EXCEPTION_NO_FILE = self::class.": file '%s' does not exists (or path is incorrect?)"; |
||
| 37 | private const EXCEPTION_READ_ERROR = self::class.": file '%s' failed to be loaded (permission denied ?)"; |
||
| 38 | private const EXCEPTION_LINE_SPLIT = self::class.": content is not a string(maybe a file error?)"; |
||
| 39 | |||
| 40 | public function __construct($absolutePath = null, $options = null, $debug = 0) |
||
| 41 | { |
||
| 42 | $this->debug = is_int($debug) ? min($debug, 3) : 1; |
||
| 43 | $this->options = is_int($options) ? $options : $this->options; |
||
| 44 | if (is_string($absolutePath)) { |
||
| 45 | $this->load($absolutePath); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Load a file and save its content as $content |
||
| 51 | * |
||
| 52 | * @param string $absolutePath The absolute path of a file |
||
| 53 | * |
||
| 54 | * @throws \Exception if file don't exist OR reading failed |
||
| 55 | * |
||
| 56 | * @return self ( returns the same Loader ) |
||
| 57 | */ |
||
| 58 | public function load(string $absolutePath):Loader |
||
| 74 | } |
||
| 75 | |||
| 76 | private function getSourceIterator($strContent = null) |
||
| 84 | } |
||
| 85 | }; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Parse Yaml lines into a hierarchy of Node |
||
| 90 | * |
||
| 91 | * @param string $strContent The Yaml string or null to parse loaded content |
||
| 92 | * |
||
| 93 | * @throws \Exception if content is not available as $strContent or as $this->content (from file) |
||
| 94 | * @throws \ParseError if any error during parsing or building |
||
| 95 | * |
||
| 96 | * @return array|YamlObject|null null on errors if NO_PARSING_EXCEPTIONS is set, otherwise an array of YamlObject or just YamlObject |
||
| 97 | */ |
||
| 98 | public function parse($strContent = null) |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | public function onMoreIndent(Node &$previous, Node &$target) |
||
| 145 | { |
||
| 146 | $deepest = $previous->getDeepestNode(); |
||
| 147 | if ($previous->type & Y::ITEM) { |
||
| 148 | if ($deepest->type & (Y::KEY|Y::TAG) && is_null($deepest->value)) { |
||
| 149 | $target = $deepest; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | private function onEqualIndent(Node &$n, Node &$previous, Node &$target) |
||
| 156 | { |
||
| 157 | $deepest = $previous->getDeepestNode(); |
||
| 158 | if ($n->type & Y::KEY && $n->indent === 0) { |
||
| 159 | $target = $previous->getParent(-1);//get root |
||
| 160 | } elseif ($n->type & Y::ITEM && $deepest->type & Y::KEY && is_null($deepest->value)) { |
||
| 161 | $target = $deepest; |
||
| 162 | } else { |
||
| 163 | $target = $previous->getParent(); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | public function attachBlankLines(array &$emptyLines, Node &$previous) |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | private function onSpecialType(Node &$n, Node &$previous, &$emptyLines, $lineString):bool |
||
| 199 | } |
||
| 200 | |||
| 201 | private function onSpecialBlank(array &$emptyLines, Node $n, Node $previous, Node $deepest) |
||
| 205 | } |
||
| 206 | |||
| 207 | private function onContextType(Node &$n, Node &$previous, $lineString):bool |
||
| 226 |