| Conditions | 11 |
| Paths | 73 |
| Total Lines | 119 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 117 | public function __construct($article) |
||
| 118 | { |
||
| 119 | self::$logger = new Logger('TCPDFFacade'); |
||
| 120 | self::$logger->debug('>>__construct()'); |
||
| 121 | |||
| 122 | $config = ConfigProvider::getInstance(); |
||
| 123 | |||
| 124 | $this->article = $article; |
||
|
|
|||
| 125 | |||
| 126 | $reflect = new \ReflectionClass($this->article); |
||
| 127 | $classname = $reflect->getShortName(); |
||
| 128 | |||
| 129 | $this->PDFFilename = $config->get('app.file.store.dir').'cache/pdf/'.$classname.'_'.$this->article->getID().'_'.$this->article->getVersion().'.pdf'; |
||
| 130 | $this->HTMLFilename = $config->get('app.file.store.dir').'cache/html/'.$classname.'_'.$this->article->getID().'_'.$this->article->getVersion().'.html'; |
||
| 131 | |||
| 132 | // first check the PDF cache |
||
| 133 | if ($this->checkPDFCache()) { |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | if (method_exists($this->article, 'getAttachmentsURL')) { |
||
| 138 | $attachURL = $this->article->getAttachmentsURL(); |
||
| 139 | } else { |
||
| 140 | $attachURL = ''; |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($this->checkHTMLCache()) { |
||
| 144 | $this->loadHTMLCache(); |
||
| 145 | } else { |
||
| 146 | $this->content = $this->markdown($this->article->get('content', true)); |
||
| 147 | $this->HTMLCache(); |
||
| 148 | } |
||
| 149 | |||
| 150 | // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller |
||
| 151 | $attachments = array(); |
||
| 152 | preg_match_all('/href\=\"\$attachURL\/.*\"/', $this->content, $attachments); |
||
| 153 | |||
| 154 | foreach ($attachments[0] as $attachmentURL) { |
||
| 155 | $start = mb_strpos($attachmentURL, '/'); |
||
| 156 | $end = mb_strrpos($attachmentURL, '"'); |
||
| 157 | $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1)); |
||
| 158 | |||
| 159 | if (method_exists($this->article, 'getAttachmentSecureURL')) { |
||
| 160 | $this->content = str_replace($attachmentURL, 'href='.$this->article->getAttachmentSecureURL($fileName), $this->content); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | // Handle image attachments |
||
| 165 | $attachments = array(); |
||
| 166 | preg_match_all('/\<img\ src\=\"\$attachURL\/.*\".*\>/', $this->content, $attachments); |
||
| 167 | |||
| 168 | foreach ($attachments[0] as $attachmentURL) { |
||
| 169 | $start = mb_strpos($attachmentURL, '/'); |
||
| 170 | $end = mb_strrpos($attachmentURL, '" alt'); |
||
| 171 | $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1)); |
||
| 172 | |||
| 173 | if ($config->get('cms.images.widget')) { |
||
| 174 | // get the details of the source image |
||
| 175 | $path = $this->article->getAttachmentsLocation().'/'.$fileName; |
||
| 176 | $image_details = getimagesize($path); |
||
| 177 | $imgType = $image_details[2]; |
||
| 178 | if ($imgType == 1) { |
||
| 179 | $type = 'gif'; |
||
| 180 | } elseif ($imgType == 2) { |
||
| 181 | $type = 'jpg'; |
||
| 182 | } else { |
||
| 183 | $type = 'png'; |
||
| 184 | } |
||
| 185 | |||
| 186 | $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (boolean) $config->get('cms.images.widget.secure')); |
||
| 187 | $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content); |
||
| 188 | } else { |
||
| 189 | // render a normal image link to the ViewAttachment controller |
||
| 190 | if (method_exists($this->article, 'getAttachmentSecureURL')) { |
||
| 191 | $this->content = str_replace($attachmentURL, '<img src="'.$this->article->getAttachmentSecureURL($fileName).'">', $this->content); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | $this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
||
| 197 | $this->pdf->SetCreator(PDF_CREATOR); |
||
| 198 | $this->pdf->SetAuthor($this->article->get('author')); |
||
| 199 | $this->pdf->SetTitle($this->article->get('title')); |
||
| 200 | $this->pdf->SetSubject($this->article->get('description')); |
||
| 201 | |||
| 202 | //set margins |
||
| 203 | $this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
||
| 204 | $this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER); |
||
| 205 | $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
||
| 206 | |||
| 207 | //set auto page breaks |
||
| 208 | $this->pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM); |
||
| 209 | |||
| 210 | //set image scale factor |
||
| 211 | $this->pdf->setImageScale(2.5); |
||
| 212 | |||
| 213 | // add a page |
||
| 214 | $this->pdf->AddPage(); |
||
| 215 | |||
| 216 | // add the title |
||
| 217 | $title = '<h1>'.$this->article->get('title').'</h1>'; |
||
| 218 | // add some custom footer info about the article |
||
| 219 | $footer = '<br><p>Article URL: <a href="'.$this->article->get('URL').'">'.$this->article->get('URL').'</a><br>Title: '.$this->article->get('title').'<br>Author: '.$this->article->get('author').'</p>'; |
||
| 220 | |||
| 221 | // write the title |
||
| 222 | self::$logger->debug('Writing the title ['.$title.'] to the PDF'); |
||
| 223 | $this->pdf->writeHTML(utf8_encode($title), true, false, true, false, ''); |
||
| 224 | // output the HTML content |
||
| 225 | self::$logger->debug('Writing the content ['.$this->content.'] to the PDF'); |
||
| 226 | $this->pdf->writeHTML(utf8_encode($this->content), true, false, true, false, ''); |
||
| 227 | // write the article footer |
||
| 228 | $this->pdf->writeHTML(utf8_encode($footer), true, false, true, false, ''); |
||
| 229 | self::$logger->debug('Writing the footer ['.$footer.'] to the PDF'); |
||
| 230 | |||
| 231 | // save this PDF to the cache |
||
| 232 | $this->pdf->Output($this->PDFFilename, 'F'); |
||
| 233 | |||
| 234 | self::$logger->debug('<<__construct()'); |
||
| 235 | } |
||
| 236 | |||
| 363 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.