| Total Complexity | 40 |
| Total Lines | 163 |
| 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 |
||
| 11 | public $errors = []; |
||
| 12 | |||
| 13 | public const EXCLUDE_DIRECTIVES = 1;//DONT include_directive |
||
| 14 | public const IGNORE_COMMENTS = 2;//DONT include_comments |
||
| 15 | public const EXCEPTIONS_PARSING = 4;//THROW Exception on parsing Errors |
||
| 16 | public const NO_OBJECT_FOR_DATE = 8;//DONT import date strings as dateTime Object |
||
| 17 | //privates |
||
| 18 | private $content;/* @var null|string */ |
||
| 19 | private $filePath;/* @var null|string */ |
||
| 20 | private $debug = 0;//TODO: determine levels |
||
| 21 | private $options = 0;/* @var int */ |
||
| 22 | //Exceptions messages |
||
| 23 | private const INVALID_VALUE = self::class.": at line %d"; |
||
| 24 | private const EXCEPTION_NO_FILE = self::class.": file '%s' does not exists (or path is incorrect?)"; |
||
| 25 | private const EXCEPTION_READ_ERROR = self::class.": file '%s' failed to be loaded (permission denied ?)"; |
||
| 26 | private const EXCEPTION_LINE_SPLIT = self::class.": content is not a string(maybe a file error?)"; |
||
| 27 | |||
| 28 | public function __construct($absolutePath = null, $options = null, $debug = 0) |
||
| 29 | { |
||
| 30 | $this->debug = is_int($debug) ? min($debug, 3) : 1; |
||
| 31 | $this->options = is_int($options) ? $options : $this->options; |
||
| 32 | if (is_string($absolutePath)) { |
||
| 33 | $this->load($absolutePath); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * load a file and save its content as $content |
||
| 39 | * |
||
| 40 | * @param string $absolutePath The absolute path of a file |
||
|
3 ignored issues
–
show
|
|||
| 41 | * |
||
| 42 | * @throws \Exception if file don't exist OR reading failed |
||
|
1 ignored issue
–
show
|
|||
| 43 | * |
||
| 44 | * @return self ( returns the same Loader ) |
||
|
1 ignored issue
–
show
|
|||
| 45 | */ |
||
| 46 | public function load(string $absolutePath):Loader |
||
| 47 | { |
||
| 48 | $this->debug && var_dump($absolutePath); |
||
|
1 ignored issue
–
show
|
|||
| 49 | $this->filePath = $absolutePath; |
||
| 50 | if (!file_exists($absolutePath)) { |
||
| 51 | throw new \Exception(sprintf(self::EXCEPTION_NO_FILE, $absolutePath)); |
||
| 52 | } |
||
| 53 | $adle = "auto_detect_line_endings"; |
||
| 54 | $prevADLE = ini_get($adle); |
||
| 55 | !$prevADLE && ini_set($adle, "true"); |
||
| 56 | $content = file($absolutePath, FILE_IGNORE_NEW_LINES); |
||
| 57 | !$prevADLE && ini_set($adle, "false"); |
||
| 58 | if (is_bool($content)) { |
||
| 59 | throw new \Exception(sprintf(self::EXCEPTION_READ_ERROR, $absolutePath)); |
||
| 60 | } |
||
| 61 | $this->content = $content; |
||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Parse Yaml lines into an hierarchy of Node |
||
| 67 | * |
||
| 68 | * @param string $strContent The Yaml string or null to parse loaded content |
||
|
3 ignored issues
–
show
|
|||
| 69 | * @throws \Exception if content is not available as $strContent or as $this->content (from file) |
||
|
1 ignored issue
–
show
|
|||
| 70 | * @throws \ParseError if any error during parsing or building |
||
|
1 ignored issue
–
show
|
|||
| 71 | * |
||
| 72 | * @return array|YamlObject the hierarchy built an array of YamlObject or just YamlObject |
||
|
1 ignored issue
–
show
|
|||
| 73 | */ |
||
| 74 | public function parse($strContent = null) |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | private function onSpecialType(&$n, &$parent, &$previous, &$emptyLines):bool |
||
| 147 | } |
||
| 148 | |||
| 149 | private function onDeepestType(&$n, &$parent, &$previous, $lineString):bool |
||
| 171 | } |
||
| 172 | } |
||
| 173 |