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