| Conditions | 7 |
| Paths | 25 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 95 | public function __construct(SplFileInfo $file = null) |
||
| 96 | { |
||
| 97 | $this->file = $file; |
||
| 98 | |||
| 99 | // physical page |
||
| 100 | if ($this->file instanceof SplFileInfo) { |
||
| 101 | /* |
||
| 102 | * File path components |
||
| 103 | */ |
||
| 104 | // ie: content/Blog/Post 1.md |
||
| 105 | // | | └─ fileExtension |
||
| 106 | // | └─ fileName |
||
| 107 | // └─ filePath |
||
| 108 | $this->fileExtension = pathinfo($this->file, PATHINFO_EXTENSION); |
||
| 109 | $this->filePath = str_replace(DIRECTORY_SEPARATOR, '/', $this->file->getRelativePath()); |
||
| 110 | $this->fileName = $this->file->getBasename('.'.$this->fileExtension); |
||
| 111 | // filePathname = filePath + '/' + fileName |
||
| 112 | // ie: "Blog/Post 1" |
||
| 113 | $this->filePathname = ($this->filePath ? $this->filePath.'/' : '') |
||
| 114 | .($this->filePath && $this->fileName == 'index' ? '' : $this->fileName); |
||
| 115 | /* |
||
| 116 | * Set properties |
||
| 117 | */ |
||
| 118 | // ID. ie: "blog/post-1" |
||
| 119 | $this->id = $this->slugify(Prefix::subPrefix($this->filePathname)); |
||
| 120 | // Path. ie: "blog" |
||
| 121 | $this->path = $this->slugify($this->filePath); |
||
| 122 | // Name. ie: "post-1" |
||
| 123 | $this->name = $this->slugify(Prefix::subPrefix($this->fileName)); |
||
| 124 | // Pathname. ie: "blog/post-1" |
||
| 125 | $this->pathname = $this->slugify(Prefix::subPrefix($this->filePathname)); |
||
| 126 | /* |
||
| 127 | * Set variables |
||
| 128 | */ |
||
| 129 | // Section. ie: "blog" |
||
| 130 | $this->setSection(explode('/', $this->path)[0]); |
||
| 131 | /* |
||
| 132 | * Set variables overridden by front matter |
||
| 133 | */ |
||
| 134 | // title. ie: "Post 1" |
||
| 135 | $this->setVariable('title', Prefix::subPrefix($this->fileName)); |
||
| 136 | // date (from file meta) |
||
| 137 | $this->setVariable('date', filemtime($this->file->getPathname())); |
||
| 138 | // weight |
||
| 139 | $this->setVariable('weight', null); |
||
| 140 | // url |
||
| 141 | $this->setVariable('url', $this->pathname.'/'); |
||
| 142 | // special case: file has a prefix |
||
| 143 | if (Prefix::hasPrefix($this->filePathname)) { |
||
| 144 | // prefix is a valid date? |
||
| 145 | if (Util::isValidDate(Prefix::getPrefix($this->filePathname))) { |
||
| 146 | $this->setVariable('date', (string) Prefix::getPrefix($this->filePathname)); |
||
| 147 | } else { |
||
| 148 | // prefix is an integer, use for sorting |
||
| 149 | $this->setVariable('weight', (int) Prefix::getPrefix($this->filePathname)); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | parent::__construct($this->id); |
||
| 154 | } else { |
||
| 155 | // virtual page |
||
| 156 | $this->virtual = true; |
||
| 157 | // date (current date) |
||
| 158 | $this->setVariable('date', time()); |
||
| 159 | |||
| 160 | parent::__construct(); |
||
| 161 | } |
||
| 162 | $this->setType(Type::PAGE); |
||
| 163 | $this->setVariable('virtual', $this->virtual); |
||
| 164 | $this->setVariable('published', true); |
||
| 165 | $this->setVariable('content_template', 'page.content.twig'); |
||
| 166 | } |
||
| 167 | |||
| 385 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..