Conditions | 20 |
Paths | 1512 |
Total Lines | 105 |
Code Lines | 46 |
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 |
||
63 | public function validate($config, $context) { |
||
64 | |||
65 | // ABNF definitions from RFC 3986 |
||
66 | $chars_sub_delims = '!$&\'()*+,;='; |
||
67 | $chars_gen_delims = ':/?#[]@'; |
||
68 | $chars_pchar = $chars_sub_delims . ':@'; |
||
69 | |||
70 | // validate host |
||
71 | if (!is_null($this->host)) { |
||
72 | $host_def = new HTMLPurifier_AttrDef_URI_Host(); |
||
73 | $this->host = $host_def->validate($this->host, $config, $context); |
||
74 | if ($this->host === false) $this->host = null; |
||
75 | } |
||
76 | |||
77 | // validate scheme |
||
78 | // NOTE: It's not appropriate to check whether or not this |
||
79 | // scheme is in our registry, since a URIFilter may convert a |
||
80 | // URI that we don't allow into one we do. So instead, we just |
||
81 | // check if the scheme can be dropped because there is no host |
||
82 | // and it is our default scheme. |
||
83 | if (!is_null($this->scheme) && is_null($this->host) || $this->host === '') { |
||
84 | // support for relative paths is pretty abysmal when the |
||
85 | // scheme is present, so axe it when possible |
||
86 | $def = $config->getDefinition('URI'); |
||
87 | if ($def->defaultScheme === $this->scheme) { |
||
88 | $this->scheme = null; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | // validate username |
||
93 | if (!is_null($this->userinfo)) { |
||
94 | $encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . ':'); |
||
95 | $this->userinfo = $encoder->encode($this->userinfo); |
||
96 | } |
||
97 | |||
98 | // validate port |
||
99 | if (!is_null($this->port)) { |
||
100 | if ($this->port < 1 || $this->port > 65535) $this->port = null; |
||
101 | } |
||
102 | |||
103 | // validate path |
||
104 | $path_parts = array(); |
||
105 | $segments_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/'); |
||
106 | if (!is_null($this->host)) { // this catches $this->host === '' |
||
107 | // path-abempty (hier and relative) |
||
108 | // http://www.example.com/my/path |
||
109 | // //www.example.com/my/path (looks odd, but works, and |
||
110 | // recognized by most browsers) |
||
111 | // (this set is valid or invalid on a scheme by scheme |
||
112 | // basis, so we'll deal with it later) |
||
113 | // file:///my/path |
||
114 | // ///my/path |
||
115 | $this->path = $segments_encoder->encode($this->path); |
||
116 | } elseif ($this->path !== '') { |
||
117 | if ($this->path[0] === '/') { |
||
118 | // path-absolute (hier and relative) |
||
119 | // http:/my/path |
||
120 | // /my/path |
||
121 | if (strlen($this->path) >= 2 && $this->path[1] === '/') { |
||
122 | // This could happen if both the host gets stripped |
||
123 | // out |
||
124 | // http://my/path |
||
125 | // //my/path |
||
126 | $this->path = ''; |
||
127 | } else { |
||
128 | $this->path = $segments_encoder->encode($this->path); |
||
129 | } |
||
130 | } elseif (!is_null($this->scheme)) { |
||
131 | // path-rootless (hier) |
||
132 | // http:my/path |
||
133 | // Short circuit evaluation means we don't need to check nz |
||
134 | $this->path = $segments_encoder->encode($this->path); |
||
135 | } else { |
||
136 | // path-noscheme (relative) |
||
137 | // my/path |
||
138 | // (once again, not checking nz) |
||
139 | $segment_nc_encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . '@'); |
||
140 | $c = strpos($this->path, '/'); |
||
141 | if ($c !== false) { |
||
142 | $this->path = |
||
143 | $segment_nc_encoder->encode(substr($this->path, 0, $c)) . |
||
144 | $segments_encoder->encode(substr($this->path, $c)); |
||
145 | } else { |
||
146 | $this->path = $segment_nc_encoder->encode($this->path); |
||
147 | } |
||
148 | } |
||
149 | } else { |
||
150 | // path-empty (hier and relative) |
||
151 | $this->path = ''; // just to be safe |
||
152 | } |
||
153 | |||
154 | // qf = query and fragment |
||
155 | $qf_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/?'); |
||
156 | |||
157 | if (!is_null($this->query)) { |
||
158 | $this->query = $qf_encoder->encode($this->query); |
||
159 | } |
||
160 | |||
161 | if (!is_null($this->fragment)) { |
||
162 | $this->fragment = $qf_encoder->encode($this->fragment); |
||
163 | } |
||
164 | |||
165 | return true; |
||
166 | |||
167 | } |
||
168 | |||
243 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.