| Conditions | 27 |
| Paths | 1026 |
| Total Lines | 96 |
| Code Lines | 57 |
| 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 |
||
| 71 | protected function addValidElements($validElements) |
||
| 72 | { |
||
| 73 | $elementRuleRegExp = '/^([#+\-])?([^\[\/]+)(?:\/([^\[]+))?(?:\[([^\]]+)\])?$/'; |
||
| 74 | $attrRuleRegExp = '/^([!\-])?(\w+::\w+|[^=:<]+)?(?:([=:<])(.*))?$/'; |
||
| 75 | $hasPatternsRegExp = '/[*?+]/'; |
||
| 76 | |||
| 77 | foreach (explode(',', $validElements) as $validElement) { |
||
| 78 | if (preg_match($elementRuleRegExp, $validElement, $matches)) { |
||
| 79 | $prefix = isset($matches[1]) ? $matches[1] : null; |
||
| 80 | $elementName = isset($matches[2]) ? $matches[2] : null; |
||
| 81 | $outputName = isset($matches[3]) ? $matches[3] : null; |
||
| 82 | $attrData = isset($matches[4]) ? $matches[4] : null; |
||
| 83 | |||
| 84 | // Create the new element |
||
| 85 | $element = new stdClass(); |
||
| 86 | $element->attributes = array(); |
||
| 87 | $element->attributePatterns = array(); |
||
| 88 | |||
| 89 | $element->attributesRequired = array(); |
||
| 90 | $element->attributesDefault = array(); |
||
| 91 | $element->attributesForced = array(); |
||
| 92 | |||
| 93 | foreach (array('#' => 'paddEmpty', '-' => 'removeEmpty') as $match => $means) { |
||
| 94 | $element->$means = ($prefix === $match); |
||
| 95 | } |
||
| 96 | |||
| 97 | // Copy attributes from global rule into current rule |
||
| 98 | if ($this->globalAttributes) { |
||
| 99 | $element->attributes = array_merge($element->attributes, $this->globalAttributes); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Attributes defined |
||
| 103 | if ($attrData) { |
||
| 104 | foreach (explode('|', $attrData) as $attr) { |
||
| 105 | if (preg_match($attrRuleRegExp, $attr, $matches)) { |
||
| 106 | $attr = new stdClass(); |
||
| 107 | |||
| 108 | $attrType = isset($matches[1]) ? $matches[1] : null; |
||
| 109 | $attrName = isset($matches[2]) ? str_replace('::', ':', $matches[2]) : null; |
||
| 110 | $prefix = isset($matches[3]) ? $matches[3] : null; |
||
| 111 | $value = isset($matches[4]) ? $matches[4] : null; |
||
| 112 | |||
| 113 | // Required |
||
| 114 | if ($attrType === '!') { |
||
| 115 | $element->attributesRequired[] = $attrName; |
||
| 116 | $attr->required = true; |
||
| 117 | } // Denied from global |
||
| 118 | elseif ($attrType === '-') { |
||
| 119 | unset($element->attributes[$attrName]); |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | |||
| 123 | // Default value |
||
| 124 | if ($prefix) { |
||
| 125 | // Default value |
||
| 126 | if ($prefix === '=') { |
||
| 127 | $element->attributesDefault[$attrName] = $value; |
||
| 128 | $attr->defaultValue = $value; |
||
| 129 | } // Forced value |
||
| 130 | elseif ($prefix === ':') { |
||
| 131 | $element->attributesForced[$attrName] = $value; |
||
| 132 | $attr->forcedValue = $value; |
||
| 133 | } // Required values |
||
| 134 | elseif ($prefix === '<') { |
||
| 135 | $attr->validValues = explode('?', $value); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | // Check for attribute patterns |
||
| 140 | if (preg_match($hasPatternsRegExp, $attrName)) { |
||
| 141 | $attr->pattern = $this->patternToRegex($attrName); |
||
| 142 | $element->attributePatterns[] = $attr; |
||
| 143 | } else { |
||
| 144 | $element->attributes[$attrName] = $attr; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | // Global rule, store away these for later usage |
||
| 151 | if (!$this->globalAttributes && $elementName == '@') { |
||
| 152 | $this->globalAttributes = $element->attributes; |
||
| 153 | } |
||
| 154 | |||
| 155 | // Handle substitute elements such as b/strong |
||
| 156 | if ($outputName) { |
||
| 157 | $element->outputName = $elementName; |
||
| 158 | $this->elements[$outputName] = $element; |
||
| 159 | } |
||
| 160 | |||
| 161 | // Add pattern or exact element |
||
| 162 | if (preg_match($hasPatternsRegExp, $elementName)) { |
||
| 163 | $element->pattern = $this->patternToRegex($elementName); |
||
| 164 | $this->elementPatterns[] = $element; |
||
| 165 | } else { |
||
| 166 | $this->elements[$elementName] = $element; |
||
| 167 | } |
||
| 339 |