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. 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 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) |
||
| 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 |
||
| 75 | |||
| 76 | private function getSourceIterator($strContent = null) |
||
| 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) |
||
| 161 | |||
| 162 | private function onSpecialType(Node &$n, Node &$previous, &$emptyLines, $lineString):bool |
||
| 187 | |||
| 188 | private function onContextType(Node &$n, Node &$previous, $lineString):bool |
||
| 206 | } |
||
| 207 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.