Conditions | 12 |
Paths | 385 |
Total Lines | 51 |
Code Lines | 25 |
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 |
||
97 | public function connect():DriverInterface{ |
||
98 | |||
99 | if(gettype($this->db) === 'resource'){ |
||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | $options = []; |
||
104 | |||
105 | $host = $this->options->host; |
||
106 | |||
107 | if(is_numeric($this->options->port)){ |
||
108 | $host .= ', '.$this->options->port; |
||
109 | } |
||
110 | |||
111 | if($this->options->database){ |
||
112 | $options['Database'] = $this->options->database; |
||
113 | } |
||
114 | |||
115 | if($this->options->username){ |
||
116 | $options['UID'] = $this->options->username; |
||
117 | } |
||
118 | |||
119 | if($this->options->password){ |
||
120 | $options['PWD'] = $this->options->password; |
||
121 | } |
||
122 | |||
123 | if($this->options->mssql_timeout){ |
||
124 | $options['LoginTimeout'] = $this->options->mssql_timeout; |
||
125 | } |
||
126 | |||
127 | if($this->options->mssql_charset){ |
||
128 | $options['CharacterSet'] = $this->options->mssql_charset; |
||
129 | } |
||
130 | |||
131 | if($this->options->mssql_encrypt){ |
||
132 | $options['Encrypt'] = $this->options->mssql_encrypt; |
||
133 | } |
||
134 | |||
135 | $this->db = sqlsrv_connect($host, $options); |
||
136 | |||
137 | if(!$this->db){ |
||
138 | $errors = sqlsrv_errors(); |
||
139 | |||
140 | if(is_array($errors) && isset($errors['SQLSTATE'], $errors['code'], $errors['message'])){ |
||
141 | throw new DriverException('db error: [MSSqlSrv]: [SQLSTATE '.$errors['SQLSTATE'].'] ('.$errors['code'].') '.$errors['message']); |
||
142 | } |
||
143 | |||
144 | throw new DriverException('db error: [MSSqlSrv]: could not connect: '.print_r($errors, true)); |
||
|
|||
145 | } |
||
146 | |||
147 | return $this; |
||
148 | } |
||
190 |