| Conditions | 22 |
| Paths | 540 |
| Total Lines | 83 |
| Code Lines | 50 |
| 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 |
||
| 73 | public function getAttributes($compiler, $attributes) |
||
| 74 | { |
||
| 75 | $_indexed_attr = array(); |
||
| 76 | if (!isset($this->mapCache[ 'option' ])) { |
||
| 77 | $this->mapCache[ 'option' ] = array_fill_keys($this->option_flags, true); |
||
| 78 | } |
||
| 79 | foreach ($attributes as $key => $mixed) { |
||
| 80 | // shorthand ? |
||
| 81 | if (!is_array($mixed)) { |
||
| 82 | // option flag ? |
||
| 83 | if (isset($this->mapCache[ 'option' ][ trim($mixed, '\'"') ])) { |
||
| 84 | $_indexed_attr[ trim($mixed, '\'"') ] = true; |
||
| 85 | // shorthand attribute ? |
||
| 86 | } elseif (isset($this->shorttag_order[ $key ])) { |
||
| 87 | $_indexed_attr[ $this->shorttag_order[ $key ] ] = $mixed; |
||
| 88 | } else { |
||
| 89 | // too many shorthands |
||
| 90 | $compiler->trigger_template_error('too many shorthand attributes', null, true); |
||
| 91 | } |
||
| 92 | // named attribute |
||
| 93 | } else { |
||
| 94 | foreach ($mixed as $k => $v) { |
||
| 95 | // option flag? |
||
| 96 | if (isset($this->mapCache[ 'option' ][ $k ])) { |
||
| 97 | if (is_bool($v)) { |
||
| 98 | $_indexed_attr[ $k ] = $v; |
||
| 99 | } else { |
||
| 100 | if (is_string($v)) { |
||
| 101 | $v = trim($v, '\'" '); |
||
| 102 | } |
||
| 103 | if (isset($this->optionMap[ $v ])) { |
||
| 104 | $_indexed_attr[ $k ] = $this->optionMap[ $v ]; |
||
| 105 | } else { |
||
| 106 | $compiler->trigger_template_error( |
||
| 107 | "illegal value '" . var_export($v, true) . |
||
| 108 | "' for option flag '{$k}'", |
||
| 109 | null, |
||
| 110 | true |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | // must be named attribute |
||
| 115 | } else { |
||
| 116 | $_indexed_attr[ $k ] = $v; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | // check if all required attributes present |
||
| 122 | foreach ($this->required_attributes as $attr) { |
||
| 123 | if (!isset($_indexed_attr[ $attr ])) { |
||
| 124 | $compiler->trigger_template_error("missing '{$attr}' attribute", null, true); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | // check for not allowed attributes |
||
| 128 | if ($this->optional_attributes !== array('_any')) { |
||
| 129 | if (!isset($this->mapCache[ 'all' ])) { |
||
| 130 | $this->mapCache[ 'all' ] = |
||
| 131 | array_fill_keys( |
||
| 132 | array_merge( |
||
| 133 | $this->required_attributes, |
||
| 134 | $this->optional_attributes, |
||
| 135 | $this->option_flags |
||
| 136 | ), |
||
| 137 | true |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | foreach ($_indexed_attr as $key => $dummy) { |
||
| 141 | if (!isset($this->mapCache[ 'all' ][ $key ]) && $key !== 0) { |
||
| 142 | $compiler->trigger_template_error("unexpected '{$key}' attribute", null, true); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | // default 'false' for all option flags not set |
||
| 147 | foreach ($this->option_flags as $flag) { |
||
| 148 | if (!isset($_indexed_attr[ $flag ])) { |
||
| 149 | $_indexed_attr[ $flag ] = false; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | if (isset($_indexed_attr[ 'nocache' ]) && $_indexed_attr[ 'nocache' ]) { |
||
| 153 | $compiler->tag_nocache = true; |
||
| 154 | } |
||
| 155 | return $_indexed_attr; |
||
| 156 | } |
||
| 204 |