Conditions | 12 |
Paths | 205 |
Total Lines | 65 |
Code Lines | 36 |
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 |
||
64 | protected function connect() |
||
65 | { |
||
66 | // determine how to get our parameters |
||
67 | $method = $this->getParameter('method', 'normal'); |
||
68 | |||
69 | // get parameters |
||
70 | switch ($method) { |
||
71 | case 'normal': |
||
72 | // get parameters normally |
||
73 | $database = $this->getParameter('database'); |
||
74 | $host = $this->getParameter('host'); |
||
75 | $password = $this->getParameter('password'); |
||
76 | $port = $this->getParameter('port'); |
||
77 | $user = $this->getParameter('username'); |
||
78 | // construct connection string |
||
79 | $string = (($database != null) ? (' dbname=' . $database) : '') . |
||
80 | (($host != null) ? (' host=' . $host) : '') . |
||
81 | (($password != null) ? (' password=' . $password) : '') . |
||
82 | (($port != null) ? (' port=' . $port) : '') . |
||
83 | (($user != null) ? (' user=' . $user) : ''); |
||
84 | break; |
||
85 | |||
86 | case 'server': |
||
87 | // construct a connection string from existing $_SERVER values |
||
88 | $string = $this->loadParameters($_SERVER); |
||
89 | break; |
||
90 | |||
91 | case 'env': |
||
92 | // construct a connection string from existing $_ENV values |
||
93 | $string = $this->loadParameters($_ENV); |
||
94 | break; |
||
95 | |||
96 | default: |
||
97 | // who knows what the user wants... |
||
98 | $error = 'Invalid AgaviPostgreSQLDatabase parameter retrieval method "%s"'; |
||
99 | $error = sprintf($error, $method); |
||
100 | throw new DatabaseException($error); |
||
101 | } |
||
102 | |||
103 | // let's see if we need a persistent connection |
||
104 | $persistent = $this->getParameter('persistent', false); |
||
105 | |||
106 | if ($persistent) { |
||
107 | $this->connection = pg_pconnect($string); |
||
108 | } else { |
||
109 | $this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW); |
||
110 | } |
||
111 | |||
112 | // make sure the connection went through |
||
113 | if ($this->connection === false) { |
||
114 | // the connection's foobar'd |
||
115 | $error = 'Failed to create a AgaviPostgreSQLDatabase connection'; |
||
116 | |||
117 | throw new DatabaseException($error); |
||
118 | } |
||
119 | |||
120 | // since we're not an abstraction layer, we copy the connection |
||
121 | // to the resource |
||
122 | $this->resource =& $this->connection; |
||
123 | |||
124 | |||
125 | foreach ((array)$this->getParameter('init_queries') as $query) { |
||
126 | pg_query($this->connection, $query); |
||
127 | } |
||
128 | } |
||
129 | |||
175 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: