Conditions | 12 |
Paths | 45 |
Total Lines | 78 |
Code Lines | 54 |
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 |
||
72 | protected function parsePropertyCell( |
||
73 | ModelInterface $object, |
||
74 | PropertyInterface $property, |
||
75 | $propertyValue |
||
76 | ) { |
||
77 | $propertyIdent = $property->ident(); |
||
78 | |||
79 | switch ($propertyIdent) { |
||
80 | case 'template_ident': |
||
81 | if ($object->templateIdent() === 'volleyball/template/page') { |
||
|
|||
82 | $propertyValue = sprintf( |
||
83 | '<span aria-hidden="true">─</span><span class="sr-only">%s</span>', |
||
84 | $this->translator()->translation([ |
||
85 | 'en' => 'Default Template', |
||
86 | 'fr' => 'Modèle par défaut' |
||
87 | ]) |
||
88 | ); |
||
89 | } |
||
90 | break; |
||
91 | |||
92 | case 'title': |
||
93 | case 'menu_label': |
||
94 | $sectionTag = null; |
||
95 | $sectionType = $object->sectionType(); |
||
96 | |||
97 | switch ($sectionType) { |
||
98 | case AbstractSection::TYPE_EXTERNAL: |
||
99 | $externalUrl = (string)$object->externalUrl(); |
||
100 | $linkExcerpt = ''; |
||
101 | $tagTemplate = '<span class="glyphicon glyphicon-link" data-toggle="tooltip" '. |
||
102 | 'data-placement="auto" title="%1$s"></span>'; |
||
103 | |||
104 | if ($externalUrl) { |
||
105 | $tagTemplate = '<a class="btn btn-default btn-xs" href="%2$s" target="_blank">'. |
||
106 | '<span class="glyphicon glyphicon-link" aria-hidden="true"></span> '. |
||
107 | '<span class="sr-only">URL:</span> %3$s'. |
||
108 | '</a>'; |
||
109 | |||
110 | $linkExcerpt = $this->abridgeUri($externalUrl); |
||
111 | } |
||
112 | |||
113 | $p = $object->p('section_type'); |
||
114 | $propertyValue .= sprintf( |
||
115 | ' '.$tagTemplate, |
||
116 | $p->displayVal($p->val()), |
||
117 | $externalUrl, |
||
118 | $linkExcerpt |
||
119 | ); |
||
120 | break; |
||
121 | } |
||
122 | break; |
||
123 | } |
||
124 | |||
125 | if ($propertyIdent === 'title') { |
||
126 | if (is_callable([ $object, 'navMenu' ]) && $object->navMenu()) { |
||
127 | $propertyValue .= sprintf( |
||
128 | ' '. |
||
129 | '<span class="glyphicon glyphicon-list" data-toggle="tooltip" '. |
||
130 | 'data-placement="auto" title="%s"></span>', |
||
131 | $this->translator()->translation([ |
||
132 | 'en' => 'Present in a menu', |
||
133 | 'fr' => 'Présent dans un menu' |
||
134 | ]) |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | if (is_callable([ $object, 'locked' ]) && $object->locked()) { |
||
139 | $propertyValue .= sprintf( |
||
140 | ' '. |
||
141 | '<span class="glyphicon glyphicon-lock" data-toggle="tooltip" '. |
||
142 | 'data-placement="auto" title="%s"></span>', |
||
143 | $object->p('locked')->label() |
||
144 | ); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | return parent::parsePropertyCell($object, $property, $propertyValue); |
||
149 | } |
||
150 | |||
205 |
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 implementation 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 interface: