Conditions | 13 |
Paths | 63 |
Total Lines | 74 |
Code Lines | 39 |
Lines | 5 |
Ratio | 6.76 % |
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 |
||
61 | public function execute(XmlConfigDomDocument $document) |
||
62 | { |
||
63 | // set up our default namespace |
||
64 | $document->setDefaultNamespace(self::XML_NAMESPACE, 'databases'); |
||
65 | |||
66 | $databases = array(); |
||
67 | $default = null; |
||
68 | foreach ($document->getConfigurationElements() as $configuration) { |
||
69 | if (!$configuration->hasChildren('databases')) { |
||
70 | continue; |
||
71 | } |
||
72 | |||
73 | $databasesElement = $configuration->getChild('databases'); |
||
74 | |||
75 | // make sure we have a default database exists |
||
76 | if (!$databasesElement->hasAttribute('default') && $default === null) { |
||
77 | // missing default database |
||
78 | $error = 'Configuration file "%s" must specify a default database configuration'; |
||
79 | $error = sprintf($error, $document->documentURI); |
||
80 | |||
81 | throw new ParseException($error); |
||
82 | } |
||
83 | if ($databasesElement->hasAttribute('default')) { |
||
84 | $default = $databasesElement->getAttribute('default'); |
||
85 | } |
||
86 | |||
87 | // let's do our fancy work |
||
88 | /** @var XmlConfigDomElement $database */ |
||
89 | foreach ($configuration->get('databases') as $database) { |
||
90 | $name = $database->getAttribute('name'); |
||
91 | |||
92 | if (!isset($databases[$name])) { |
||
93 | $databases[$name] = array('parameters' => array()); |
||
94 | |||
95 | if (!$database->hasAttribute('class')) { |
||
96 | $error = 'Configuration file "%s" specifies database "%s" with missing class key'; |
||
97 | $error = sprintf($error, $document->documentURI, $name); |
||
98 | |||
99 | throw new ParseException($error); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | $databases[$name]['class'] = $database->hasAttribute('class') ? $database->getAttribute('class') : $databases[$name]['class']; |
||
104 | |||
105 | $databases[$name]['parameters'] = $database->getAgaviParameters($databases[$name]['parameters']); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (!$databases) { |
||
|
|||
110 | // we have no connections |
||
111 | $error = 'Configuration file "%s" does not contain any database connections.'; |
||
112 | $error = sprintf($error, $document->documentURI); |
||
113 | throw new ConfigurationException($error); |
||
114 | } |
||
115 | |||
116 | $data = array(); |
||
117 | |||
118 | foreach ($databases as $name => $db) { |
||
119 | // append new data |
||
120 | $data[] = sprintf('$database = new %s();', $db['class']); |
||
121 | $data[] = sprintf('$this->databases[%s] = $database;', var_export($name, true)); |
||
122 | $data[] = sprintf('$database->initialize($this, %s);', var_export($db['parameters'], true)); |
||
123 | } |
||
124 | |||
125 | View Code Duplication | if (!isset($databases[$default])) { |
|
126 | $error = 'Configuration file "%s" specifies undefined default database "%s".'; |
||
127 | $error = sprintf($error, $document->documentURI, $default); |
||
128 | throw new ConfigurationException($error); |
||
129 | } |
||
130 | |||
131 | $data[] = sprintf('$this->defaultDatabaseName = %s;', var_export($default, true)); |
||
132 | |||
133 | return $this->generate($data, $document->documentURI); |
||
134 | } |
||
135 | } |
||
136 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.