Conditions | 11 |
Paths | 1 |
Total Lines | 81 |
Code Lines | 50 |
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 |
||
66 | function init_db(&$m_di, $config){ |
||
67 | $m_di->remove('db'); |
||
68 | $m_di->remove('dbCDR'); |
||
69 | $m_di->remove('dbLog'); |
||
70 | |||
71 | $m_di->set('db', function() use ($config) { |
||
72 | $db_class = '\Phalcon\Db\Adapter\Pdo\\'.$config['database']['adapter']; |
||
73 | $connection= new $db_class(array("dbname" => $config['database']['dbfile'])); |
||
74 | |||
75 | if (is_file('/tmp/debug')) { |
||
76 | $logpath = Cdr::getPathtoLog(); |
||
77 | $logger = new FileLogger($logpath); |
||
78 | $eventsManager = new Manager(); |
||
79 | // Слушаем все события базы данных |
||
80 | $eventsManager->attach('db', function ($event, $connection) use ($logger) { |
||
81 | if ($event->getType() === 'beforeQuery') { |
||
82 | $statement = $connection->getSQLStatement(); |
||
83 | $variables = $connection->getSqlVariables(); |
||
84 | if (is_array($variables)) { |
||
85 | foreach ($variables as $variable => $value) { |
||
86 | if (is_array($value)){ |
||
87 | $value = '('.implode(', ',$value).')'; |
||
88 | } |
||
89 | $variable = str_replace(':','',$variable); |
||
90 | $statement = str_replace(":$variable", "'$value'", $statement); |
||
91 | } |
||
92 | } |
||
93 | $logger->log($statement, Logger::SPECIAL); |
||
94 | } |
||
95 | }); |
||
96 | |||
97 | // Назначаем EventsManager экземпляру адаптера базы данных |
||
98 | $connection->setEventsManager($eventsManager); |
||
99 | } |
||
100 | return $connection; |
||
101 | }); |
||
102 | // Asterisk CDR Database connection. |
||
103 | $m_di->set('dbCDR', function() use ($config){ |
||
104 | $dbclass = 'Phalcon\Db\Adapter\Pdo\\'.$config['cdrdatabase']['adapter']; |
||
105 | |||
106 | /** @var \Phalcon\Db\Adapter\Pdo\Sqlite $connection */ |
||
107 | $connection = new $dbclass(array( |
||
108 | "dbname" => $config['cdrdatabase']['dbfile'] |
||
109 | )); |
||
110 | if (is_file('/tmp/debug')) { |
||
111 | $logpath = Cdr::getPathtoLog(); |
||
112 | $logger = new FileLogger($logpath); |
||
113 | $eventsManager = new Manager(); |
||
114 | // Слушаем все события базы данных |
||
115 | $eventsManager->attach('db', function ($event, $connection) use ($logger) { |
||
116 | if ($event->getType() === 'beforeQuery') { |
||
117 | $statement = $connection->getSQLStatement(); |
||
118 | $variables = $connection->getSqlVariables(); |
||
119 | if (is_array($variables)) { |
||
120 | foreach ($variables as $variable => $value) { |
||
121 | if (is_array($value)){ |
||
122 | $value = '('.implode(', ',$value).')'; |
||
123 | } |
||
124 | $variable = str_replace(':','',$variable); |
||
125 | $statement = str_replace(":$variable", "'$value'", $statement); |
||
126 | } |
||
127 | } |
||
128 | $logger->log($statement, Logger::SPECIAL); |
||
129 | } |
||
130 | }); |
||
131 | |||
132 | // Назначаем EventsManager экземпляру адаптера базы данных |
||
133 | $connection->setEventsManager($eventsManager); |
||
134 | } |
||
135 | return $connection; |
||
136 | }); |
||
137 | $m_di->set('dbLog', function() use ($config){ |
||
138 | $dbclass = 'Phalcon\Db\Adapter\Pdo\\'.$config['logdatabase']['adapter']; |
||
139 | $connection= new $dbclass(array( |
||
140 | "dbname" => $config['logdatabase']['dbfile'] |
||
141 | )); |
||
142 | return $connection; |
||
143 | }); |
||
144 | |||
145 | // Подключаем db файлы модулей как севрисы в DI |
||
146 | Modules\DiServicesInstall::Register($m_di); |
||
147 | } |
||
276 | } |
This check looks for imports that have been defined, but are not used in the scope.