| Conditions | 12 |
| Paths | 321 |
| Total Lines | 83 |
| Code Lines | 45 |
| Lines | 18 |
| Ratio | 21.69 % |
| 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 |
||
| 109 | protected function beforeRender() |
||
| 110 | { |
||
| 111 | // Check if control has template |
||
| 112 | if (!$this->template instanceof Nette\Bridges\ApplicationLatte\Template) { |
||
| 113 | throw new Exceptions\InvalidStateException('Widgets container control is without template.'); |
||
| 114 | } |
||
| 115 | |||
| 116 | // Get widget title |
||
| 117 | $name = $this->getTitle(); |
||
| 118 | |||
| 119 | // If widget name has space... |
||
| 120 | $pos = mb_strpos($name, ' '); |
||
| 121 | if ($pos !== FALSE && mb_strpos($name, '||') === FALSE) { |
||
| 122 | $name = Utils\Html::el('span') |
||
| 123 | ->addAttributes(['class' => 'color']) |
||
| 124 | ->setText(mb_substr($name, 0, $pos)) |
||
| 125 | ->render(); |
||
| 126 | |||
| 127 | // Modify widget name |
||
| 128 | $name = $name . mb_substr($name, $pos); |
||
| 129 | } |
||
| 130 | |||
| 131 | // If widget name has subtitle... |
||
| 132 | $pos = mb_strpos($name, '||'); |
||
| 133 | if ($pos !== FALSE) { |
||
| 134 | $title = Utils\Html::el('span') |
||
| 135 | ->addAttributes(['class' => 'title']) |
||
| 136 | ->setText(mb_substr($name, 0, $pos)) |
||
| 137 | ->render(); |
||
| 138 | |||
| 139 | $subtitle = Utils\Html::el('span') |
||
| 140 | ->addAttributes(['class' => 'subtitle']) |
||
| 141 | ->setText(mb_substr($name, $pos + 2)) |
||
| 142 | ->render(); |
||
| 143 | |||
| 144 | // Split name to title & subtitle |
||
| 145 | $name = $title . $subtitle; |
||
| 146 | } |
||
| 147 | |||
| 148 | // Set badge if exists |
||
| 149 | View Code Duplication | if ($badge = $this->data->getBadge()) { |
|
| 150 | $badge = Utils\Html::el('span') |
||
| 151 | ->addAttributes(['class' => 'badge badge-' . $badge]) |
||
| 152 | ->render(); |
||
| 153 | } |
||
| 154 | |||
| 155 | // Set icon if exists |
||
| 156 | View Code Duplication | if ($icon = $this->data->getIcon()) { |
|
| 157 | $icon = Utils\Html::el('span') |
||
| 158 | ->addAttributes(['class' => 'icon icon-' . $icon]) |
||
| 159 | ->render(); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Assign basic widget data to template |
||
| 163 | $this->template->add('badge', $badge); |
||
| 164 | $this->template->add('icon', $icon); |
||
| 165 | $this->template->add('title', [ |
||
| 166 | 'text' => $name, |
||
| 167 | 'insert' => $this->data->getParam('widget.title.insert', TRUE), |
||
| 168 | 'hidden' => $this->data->getParam('widget.title.hidden', FALSE) |
||
| 169 | ]); |
||
| 170 | |||
| 171 | // Check if translator is available |
||
| 172 | if ($this->getTranslator() instanceof Localization\ITranslator) { |
||
| 173 | $this->template->setTranslator($this->getTranslator()); |
||
| 174 | } |
||
| 175 | |||
| 176 | $templateFile = $this->getTemplate()->getFile(); |
||
| 177 | |||
| 178 | if (is_callable($templateFile)) { |
||
| 179 | $templateFile = call_user_func($templateFile); |
||
| 180 | } |
||
| 181 | |||
| 182 | // If template was not defined before... |
||
| 183 | View Code Duplication | if ($templateFile === NULL) { |
|
| 184 | // Get component actual dir |
||
| 185 | $dir = dirname($this->getReflection()->getFileName()); |
||
| 186 | |||
| 187 | // ...try to get base component template file |
||
| 188 | $templateFile = $this->templateFile !== NULL && is_file($this->templateFile) ? $this->templateFile : $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte'; |
||
| 189 | $this->template->setFile($templateFile); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 216 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.