| Conditions | 13 |
| Paths | 260 |
| Total Lines | 50 |
| Code Lines | 29 |
| 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 |
||
| 9 | public function __construct($target) |
||
| 10 | { |
||
| 11 | if ($target === null) { |
||
| 12 | $target = 'tcp://127.0.0.1'; |
||
| 13 | } |
||
| 14 | |||
| 15 | if (strpos($target, '://') === false) { |
||
| 16 | $target = 'tcp://' . $target; |
||
| 17 | } |
||
| 18 | |||
| 19 | $parts = parse_url($target); |
||
| 20 | if ($parts === false || !isset($parts['host']) || $parts['scheme'] !== 'tcp') { |
||
| 21 | throw new InvalidArgumentException('Given URL can not be parsed'); |
||
| 22 | } |
||
| 23 | |||
| 24 | if (!isset($parts['port'])) { |
||
| 25 | $parts['port'] = 6379; |
||
| 26 | $this->port = 6379; |
||
|
|
|||
| 27 | } |
||
| 28 | |||
| 29 | if ($parts['host'] === 'localhost') { |
||
| 30 | $parts['host'] = '127.0.0.1'; |
||
| 31 | $this->host = '127.0.0.1'; |
||
| 32 | } |
||
| 33 | |||
| 34 | $auth = null; |
||
| 35 | if (isset($parts['user'])) { |
||
| 36 | $auth = $parts['user']; |
||
| 37 | $this->auth = $auth; |
||
| 38 | } |
||
| 39 | |||
| 40 | if (isset($parts['pass'])) { |
||
| 41 | $auth .= ':' . $parts['pass']; |
||
| 42 | } |
||
| 43 | |||
| 44 | if ($auth !== null) { |
||
| 45 | $parts['auth'] = $auth; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (isset($parts['path']) && $parts['path'] !== '') { |
||
| 49 | $parts['db'] = substr($parts['path'], 1); |
||
| 50 | } |
||
| 51 | |||
| 52 | unset($parts['scheme'], $parts['user'], $parts['pass'], $parts['path']); |
||
| 53 | |||
| 54 | $this->host = $parts['host']; |
||
| 55 | $this->port = $parts['port']; |
||
| 56 | $this->auth = $auth; |
||
| 57 | $this->db = $parts['db']; |
||
| 58 | } |
||
| 59 | |||
| 74 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: