| Conditions | 40 |
| Paths | 868 |
| Total Lines | 170 |
| Code Lines | 126 |
| Lines | 28 |
| Ratio | 16.47 % |
| Tests | 0 |
| CRAP Score | 1640 |
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 |
||
| 13 | function compile_tree(&$node, $arguments=null) { |
||
| 14 | |||
| 15 | if ($arguments) { |
||
| 16 | extract($arguments); |
||
| 17 | } |
||
| 18 | |||
| 19 | switch ((string)$node["id"]) { |
||
| 20 | case 'implements': |
||
| 21 | $result="prop_object.AR_implements"; |
||
| 22 | break; |
||
| 23 | case 'property': |
||
| 24 | $table=$node["table"]; |
||
| 25 | $field=$node["field"]; |
||
| 26 | $result="$table.$field"; |
||
| 27 | break; |
||
| 28 | case 'ident': |
||
| 29 | $table=$node["table"]; |
||
| 30 | $field=$node["field"]; |
||
| 31 | $result="prop_object.AR_$field"; |
||
| 32 | break; |
||
| 33 | case 'custom': |
||
| 34 | $table="prop_custom"; |
||
| 35 | $field=$node["field"]; |
||
| 36 | $result="prop_my.AR_$field"; |
||
| 37 | break; |
||
| 38 | case 'string': |
||
| 39 | // LDAP filters don't have quotes around strings |
||
| 40 | $result=$node["value"]; |
||
| 41 | $result=substr($result, 1, -1); |
||
| 42 | break; |
||
| 43 | case 'float': |
||
| 44 | case 'int': |
||
| 45 | $result=$node["value"]; |
||
| 46 | break; |
||
| 47 | View Code Duplication | case 'and': |
|
| 48 | if (preg_match("/^&[0-9]+$/", $this->buildlist[sizeof($this->buildlist)-1]) && |
||
| 49 | !$this->groupingflag) { |
||
| 50 | $this->buildlist[sizeof($this->buildlist)-1]="&". |
||
| 51 | (substr($this->buildlist[sizeof($this->buildlist)-1], 1)+1); |
||
| 52 | } else { |
||
| 53 | array_push($this->buildlist, '&2'); |
||
| 54 | $this->groupingflag=false; |
||
| 55 | } |
||
| 56 | $this->compile_tree($node["left"]); |
||
| 57 | $this->compile_tree($node["right"]); |
||
| 58 | break; |
||
| 59 | View Code Duplication | case 'or': |
|
| 60 | if (preg_match("/^\|[0-9]+/$", $this->buildlist[sizeof($this->buildlist)-1]) && |
||
| 61 | !$this->groupingflag) { |
||
| 62 | $this->buildlist[sizeof($this->buildlist)-1]="|". |
||
| 63 | (substr($this->buildlist[sizeof($this->buildlist)-1], 1)+1); |
||
| 64 | } else { |
||
| 65 | array_push($this->buildlist, '|2'); |
||
| 66 | $this->groupingflag=false; |
||
| 67 | } |
||
| 68 | $this->compile_tree($node["left"]); |
||
| 69 | $this->compile_tree($node["right"]); |
||
| 70 | break; |
||
| 71 | case 'cmp': |
||
| 72 | $not=false; |
||
| 73 | $joker=false; |
||
| 74 | $operator=$node["operator"]; |
||
| 75 | switch ($operator) { |
||
| 76 | case '!~': |
||
| 77 | case '!~~': |
||
| 78 | $not=true; |
||
| 79 | case '~=': |
||
| 80 | case '=~': |
||
| 81 | case '=~~': |
||
| 82 | $joker=true; |
||
| 83 | $ecompare = '='; |
||
| 84 | break; |
||
| 85 | case '!=': |
||
| 86 | $not=true; |
||
| 87 | // fall through |
||
| 88 | case '=': |
||
| 89 | case '==': |
||
| 90 | $ecompare = '='; |
||
| 91 | break; |
||
| 92 | case '>': |
||
| 93 | $not=true; |
||
| 94 | // fall through |
||
| 95 | case '<=': |
||
| 96 | $ecompare = '<='; |
||
| 97 | break; |
||
| 98 | case '<': |
||
| 99 | $not=true; |
||
| 100 | // fall through |
||
| 101 | case '>=': |
||
| 102 | $ecompare = '>='; |
||
| 103 | break; |
||
| 104 | } |
||
| 105 | $left=$this->compile_tree($node["left"]); |
||
| 106 | $right=$this->compile_tree($node["right"]); |
||
| 107 | |||
| 108 | // Quote the characters that have a special meaning in LDAP filters |
||
| 109 | // as per RFC 2254 |
||
| 110 | $ldapspecial=array("\\", "*", "(", ")", "\x00"); |
||
| 111 | $ldapreplace=array("\\5c", "\\2a", "\\28", "\\29", "\\00"); |
||
| 112 | $evalue=str_replace($ldapspecial, $ldapreplace, $right); |
||
| 113 | |||
| 114 | if ($joker) { |
||
| 115 | // Replace '%' with '*' for the LDAP filter |
||
| 116 | $evalue=str_replace('%', '*', $evalue); |
||
| 117 | } |
||
| 118 | |||
| 119 | if (isset($this->mappings["$left$operator$right"])) { |
||
| 120 | // Complete translation (property name and value) |
||
| 121 | $result=$this->mappings["$left$operator$right"]; |
||
| 122 | } elseif (isset($this->mappings["$left"])) { |
||
| 123 | // Only the property name should be translated |
||
| 124 | $result=$this->mappings["$left"].$ecompare.$evalue; |
||
| 125 | } elseif (substr($left, 0, 13)=="prop_ldap.AR_") { |
||
| 126 | // Literal ldap attribute name given |
||
| 127 | $result=substr($left, 13).$ecompare.$evalue; |
||
| 128 | } else { |
||
| 129 | // Hmm, unknown property, maybe we should throw |
||
| 130 | // an error here? (TODO) |
||
| 131 | $result="$left$ecompare$evalue"; |
||
| 132 | } |
||
| 133 | |||
| 134 | // Ignore the parent property, is it makes |
||
| 135 | // no sense here (objectclass=* matches |
||
| 136 | // every LDAP object) |
||
| 137 | if ($left=='prop_object.AR_parent') { |
||
| 138 | $result='objectclass=*'; |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($not) { |
||
| 142 | $result="!($result)"; |
||
| 143 | } |
||
| 144 | |||
| 145 | array_push($this->buildlist, $result); |
||
| 146 | break; |
||
| 147 | case 'group': |
||
| 148 | $this->groupingflag=true; |
||
| 149 | $left=$this->compile_tree($node["left"]); |
||
| 150 | break; |
||
| 151 | |||
| 152 | View Code Duplication | case 'orderby': |
|
| 153 | $result=$this->compile_tree($node["left"]); |
||
| 154 | $this->orderby_s=$this->compile_tree($node["right"]); |
||
| 155 | break; |
||
| 156 | |||
| 157 | case 'orderbyfield': |
||
| 158 | $this->in_orderby = true; |
||
| 159 | $left=$this->compile_tree($node["left"]); |
||
| 160 | $right=$this->compile_tree($node["right"]); |
||
| 161 | if (isset($this->mappings["$right"])) { |
||
| 162 | // Property name should be translated |
||
| 163 | $right=$this->mappings["$right"]; |
||
| 164 | } elseif (substr($right, 0, 13)=="prop_ldap.AR_") { |
||
| 165 | // Literal ldap attribute name given |
||
| 166 | $right=substr($right, 13); |
||
| 167 | } else { |
||
| 168 | // Hmm, unknown property, maybe we should throw |
||
| 169 | // an error here? (TODO) |
||
| 170 | } |
||
| 171 | $this->orderbyfield=$right; |
||
| 172 | break; |
||
| 173 | |||
| 174 | case 'limit': |
||
| 175 | $this->compile_tree($node["left"]); |
||
| 176 | // Ignore limit and offset as LDAP does |
||
| 177 | // not support it as complete as we need it |
||
| 178 | break; |
||
| 179 | } |
||
| 180 | |||
| 181 | return $result; |
||
| 182 | } |
||
| 183 | |||
| 217 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.