| Conditions | 13 |
| Paths | 144 |
| Total Lines | 77 |
| 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 |
||
| 89 | public function __construct($record, $useCache = true) |
||
| 90 | { |
||
| 91 | $config = ConfigProvider::getInstance(); |
||
| 92 | $cache = ServiceFactory::getInstance('Alpha\Util\Cache\CacheProviderFile', 'Alpha\Util\Cache\CacheProviderInterface'); |
||
| 93 | |||
| 94 | $this->record = $record; |
||
| 95 | |||
| 96 | if ($this->record instanceof \Alpha\Model\Article && $this->record->isLoadedFromFile()) { |
||
| 97 | $underscoreTimeStamp = str_replace(array('-', ' ', ':'), '_', $this->record->getContentFileDate()); |
||
| 98 | $this->cacheKey = 'html_'.$this->record->getFriendlyClassName().'_'.$this->record->get('title').'_'.$underscoreTimeStamp; |
||
| 99 | } else { |
||
| 100 | $this->cacheKey = 'html_'.$this->record->getFriendlyClassName().'_'.$this->record->getID().'_'.$this->record->getVersion(); |
||
| 101 | } |
||
| 102 | |||
| 103 | if (!$useCache) { |
||
| 104 | $this->content = $this->markdown($this->record->get('content', true)); |
||
| 105 | } else { |
||
| 106 | if ($cache->get($this->cacheKey) !== false) { |
||
| 107 | $this->content = $cache->get($this->cacheKey); |
||
| 108 | } else { |
||
| 109 | if ($this->record->get('content', true) == '') { |
||
| 110 | // the content may not be loaded from the DB at this stage due to a previous soft-load |
||
| 111 | $this->record->reload(); |
||
| 112 | } |
||
| 113 | |||
| 114 | $this->content = $this->markdown($this->record->get('content', true)); |
||
| 115 | |||
| 116 | $cache->set($this->cacheKey, $this->content); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller |
||
| 121 | $attachments = array(); |
||
| 122 | preg_match_all('/href\=\"\$attachURL\/.*\"/', $this->content, $attachments); |
||
| 123 | |||
| 124 | foreach ($attachments[0] as $attachmentURL) { |
||
| 125 | $start = mb_strpos($attachmentURL, '/'); |
||
| 126 | $end = mb_strrpos($attachmentURL, '"'); |
||
| 127 | $fileName = mb_substr($attachmentURL, $start+1, $end-($start+1)); |
||
| 128 | |||
| 129 | if (method_exists($this->record, 'getAttachmentSecureURL')) { |
||
| 130 | $this->content = str_replace($attachmentURL, 'href="'.$this->record->getAttachmentSecureURL($fileName).'" rel="nofollow"', $this->content); |
||
|
|
|||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | // Handle image attachments |
||
| 135 | $attachments = array(); |
||
| 136 | preg_match_all('/\<img\ src\=\"\$attachURL\/.*\.[a-zA-Z]{3}\"[^<]*/', $this->content, $attachments); |
||
| 137 | |||
| 138 | foreach ($attachments[0] as $attachmentURL) { |
||
| 139 | preg_match('/\/.*\.[a-zA-Z]{3}/', $attachmentURL, $matches); |
||
| 140 | $fileName = $matches[0]; |
||
| 141 | |||
| 142 | if ($config->get('cms.images.widget')) { |
||
| 143 | // get the details of the source image |
||
| 144 | $path = $this->record->getAttachmentsLocation().$fileName; |
||
| 145 | $image_details = getimagesize($path); |
||
| 146 | $imgType = $image_details[2]; |
||
| 147 | if ($imgType == 1) { |
||
| 148 | $type = 'gif'; |
||
| 149 | } elseif ($imgType == 2) { |
||
| 150 | $type = 'jpg'; |
||
| 151 | } else { |
||
| 152 | $type = 'png'; |
||
| 153 | } |
||
| 154 | |||
| 155 | $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (boolean)$config->get('cms.images.widget.secure')); |
||
| 156 | |||
| 157 | $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content); |
||
| 158 | } else { |
||
| 159 | // render a normal image link to the ViewAttachment controller |
||
| 160 | if (method_exists($this->record, 'getAttachmentSecureURL')) { |
||
| 161 | $this->content = str_replace($attachmentURL, '<img src="'.$this->record->getAttachmentSecureURL($fileName).'">', $this->content); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 206 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: