core/Group/Dao/Dao.php
<?php namespace Group\Dao; use Group\Dao\ExtendedPdo;use Group\Dao\ConnectionLocator; class Dao{ protected $config; private static $connection; public function __construct() { $pdo = \Config::get('database::pdo'); $this->config = $pdo; } /** * 获取默认服务器连接 * * @return object */ public function getDefault() { return $this->logger($this->getConnection()->getDefault()); } /** * 获取某个读服务器连接 * * @param name * @return object */ public function getRead($name = null) { return $this->logger($this->getConnection()->getRead($name)); } /** * 获取某个写服务器连接 * * @param name * @return object */ public function getWrite($name = null) { return $this->logger($this->getConnection()->getWrite($name)); } /** * 获取所有读服务器的连接 * * @return array */ public function getAllRead() { $config = $this->config; $connections = []; if (isset($config['read'])) {Avoid unused local variables such as '$db'. foreach ($config['read'] as $name => $db) { $connections[] = $this->getRead($name); } } return $connections; } /** * 获取所有写服务器的连接 * * @return array */ public function getAllWrite() { $config = $this->config; $connections = []; if (isset($config['write'])) {Avoid unused local variables such as '$db'. foreach ($config['write'] as $name => $db) { $connections[] = $this->getWrite($name); } } return $connections; } /** * 执行sql * * @param sql * @param type[write|all_write|read|all_read|default] * @param name */Function `querySql` has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. public function querySql($sql, $type, $name = null) { switch ($type) { case 'write':Terminating statement must be indented to the same level as the CASE body return $this->getWrite($name)->query($sql); break; case 'all_write': $connections = $this->getAllWrite();Line indented incorrectly; expected 16 spaces, found 20 foreach ($connections as $connection) { $connection->query($sql);Line indented incorrectly; expected 16 spaces, found 20 } break; case 'read':Terminating statement must be indented to the same level as the CASE body return $this->getRead($name)->query($sql); break; case 'all_read': $connections = $this->getAllRead();Line indented incorrectly; expected 16 spaces, found 20 foreach ($connections as $connection) { $connection->query($sql);Line indented incorrectly; expected 16 spaces, found 20 } break; case 'default':Terminating statement must be indented to the same level as the CASE body return $this->getDefault()->query($sql); break; default: break; } } protected function getConnection() { if (self::$connection) { return self::$connection; } $connection = $this->getConnectionLocator(); self::$connection = $connection; return $connection; } Function `getConnectionLocator` has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. private function getConnectionLocator() { $config = $this->config; $connections = new ConnectionLocator; if (isset($config['default'])) { $connections->setDefault(function () use ($config) {Missing class import via use statement (line '146', column '92').
Line exceeds 120 characters; contains 123 characters return \Doctrine\DBAL\DriverManager::getConnection($config['default'], new \Doctrine\DBAL\Configuration()); }); } if (isset($config['write'])) { foreach ($config['write'] as $name => $db) { $connections->setWrite($name, function () use ($db) {Missing class import via use statement (line '153', column '81'). return \Doctrine\DBAL\DriverManager::getConnection($db, new \Doctrine\DBAL\Configuration()); }); } } if (isset($config['read'])) { foreach ($config['read'] as $name => $db) { $connections->setRead($name, function () use ($db) {Missing class import via use statement (line '161', column '81'). return \Doctrine\DBAL\DriverManager::getConnection($db, new \Doctrine\DBAL\Configuration()); }); } } return $connections; } private function logger($connection)Whitespace found at end of line { if (app('container')->isDebug() && !app('container') ->runningInConsole()) { $connection->getConfiguration()->setSQLLogger(app('debugStack')); } // if ($connection->ping() === false) { // $connection->close(); // $connection->connect(); // } return $connection; } Function `search` has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring. protected function search(&$queryBuilder, $andWhere, $condition) { foreach ($andWhere as $key => $value) { if (isset($condition[$key])) { if ($this->isInCondition($value)) { $marks = array();Inline control structures are not allowed if (empty($condition[$key])) continue; foreach (array_values($condition[$key]) as $index => $one) { $marks[] = ":{$key}_{$index}"; $condition["{$key}_{$index}"] = $one; } $value = str_replace(":{$key}", join(',', $marks), $value); unset($condition[$key]); } $queryBuilder->andWhere($value); } } foreach ($condition as $key => $value) { $queryBuilder->setParameter(":{$key}", $value); } } protected function bindValues($stmt, $conditions, $names) { foreach ($names as $name) { if (!empty($name)) { $stmt->bindValue($name, $conditions[$name]); } } $stmt->execute(); return $stmt; } private function isInCondition($where) {Avoid unused local variables such as '$matches'. $matched = preg_match('/\s+(IN)\s+/', $where, $matches); if (empty($matched)) { return false;The method isInCondition uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them. } else { return true; } }}