| Conditions | 9 |
| Paths | 26 |
| Total Lines | 60 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 48 | public function __construct (string $host = "", int $port = 0, bool $ssl = false) { |
||
| 49 | $ldap_config = array( |
||
| 50 | 'enabled' => true, |
||
| 51 | 'ssl' => $ssl |
||
| 52 | ); |
||
| 53 | |||
| 54 | if (empty($host)) { |
||
| 55 | //load local config |
||
| 56 | if (!file_exists(CONFIG_PATH . "ldap.php")) { |
||
| 57 | throw new IllegalStateException("No ldap configuration file config/ldap.php exists!"); |
||
| 58 | } |
||
| 59 | |||
| 60 | //override $ldap_config |
||
| 61 | require(CONFIG_PATH . "ldap.php"); |
||
| 62 | |||
| 63 | //check, if ldap is enabled |
||
| 64 | if ($ldap_config['enabled'] == false) { |
||
| 65 | throw new IllegalStateException("LDAP is disabled."); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->host = $ldap_config['host']; |
||
| 69 | $this->port = intval($ldap_config['port']); |
||
| 70 | |||
| 71 | $this->readonly = boolval($ldap_config['readonly']); |
||
| 72 | } else { |
||
| 73 | $this->host = $host; |
||
| 74 | $this->port = $port; |
||
| 75 | } |
||
| 76 | |||
| 77 | //check, if SSL is enabled |
||
| 78 | if ($ldap_config['ssl'] == true) { |
||
| 79 | //use OpenLDAP 2.x.x URI instead of host |
||
| 80 | $this->uri = "ldaps://" . $this->host . ":" . $this->port; |
||
| 81 | |||
| 82 | //set flag, that uri is used |
||
| 83 | $this->uri_used = true; |
||
| 84 | } |
||
| 85 | |||
| 86 | //check, if host / uri is valide (this statement doesnt connect to server!) - see also http://php.net/manual/de/function.ldap-connect.php |
||
| 87 | if ($this->uri_used) { |
||
| 88 | $this->conn = ldap_connect($this->uri); |
||
| 89 | } else { |
||
| 90 | $this->conn = ldap_connect($this->host, $this->port); |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($this->conn === FALSE) { |
||
| 94 | throw new IllegalStateException("LDAP connection parameters (host or port) are invalide."); |
||
| 95 | } |
||
| 96 | |||
| 97 | $this->dn = $ldap_config['dn']; |
||
| 98 | |||
| 99 | //set ldap params |
||
| 100 | if (isset($ldap_config['params'])) { |
||
| 101 | foreach ($ldap_config['params'] as $key=>$value) { |
||
| 102 | // configure ldap params |
||
| 103 | ldap_set_option($this->conn,$key, $value); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | $this->ldap_config = $ldap_config; |
||
| 108 | } |
||
| 196 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.