| Conditions | 17 |
| Paths | 49 |
| Total Lines | 103 |
| Code Lines | 59 |
| 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 |
||
| 47 | protected function getProcess($sqlOrFilename, $action = self::EXECUTE_COMMAND) |
||
| 48 | { |
||
| 49 | $clientType = $this->getDbClientType($this->databaseConfiguration); |
||
| 50 | |||
| 51 | // pass on _all_ env vars, including PATH. Not doing so is deprecated... |
||
| 52 | $env = null; |
||
| 53 | |||
| 54 | switch ($clientType) { |
||
| 55 | case 'mysql': |
||
| 56 | $command = 'mysql'; |
||
| 57 | $options = [ |
||
| 58 | '--host=' . $this->databaseConfiguration['host'], |
||
| 59 | '--port=' . $this->databaseConfiguration['port'] ?? '3306', |
||
| 60 | '--user=' . $this->databaseConfiguration['user'], |
||
| 61 | '-p' . $this->databaseConfiguration['password'], |
||
| 62 | '--binary-mode', // 'It also disables all mysql commands except charset and delimiter in non-interactive mode (for input piped to mysql or loaded using the source command)' |
||
| 63 | '-t', |
||
| 64 | ]; |
||
| 65 | if (isset($this->databaseConfiguration['dbname'])) { |
||
| 66 | $options[] = $this->databaseConfiguration['dbname']; |
||
| 67 | } |
||
| 68 | if ($action == self::EXECUTE_COMMAND) { |
||
| 69 | $options[] = '--execute=' . $sqlOrFilename; |
||
| 70 | } |
||
| 71 | // $env = [ |
||
| 72 | // problematic when wrapping the process in a call to `time`... |
||
| 73 | //'MYSQL_PWD' => $this->databaseConfiguration['password'], |
||
| 74 | //]; |
||
| 75 | break; |
||
| 76 | |||
| 77 | case 'psql': |
||
| 78 | $command = 'psql'; |
||
| 79 | $connectString = "postgresql://".$this->databaseConfiguration['user'].":".$this->databaseConfiguration['password']. |
||
| 80 | "@{$this->databaseConfiguration['host']}:".($this->databaseConfiguration['port'] ?? '5432').'/'; |
||
| 81 | if (isset($this->databaseConfiguration['dbname'])) { |
||
| 82 | $connectString .= $this->databaseConfiguration['dbname']; |
||
| 83 | } |
||
| 84 | $options = [ |
||
| 85 | $connectString, |
||
| 86 | '-Pfooter=off' |
||
| 87 | ]; |
||
| 88 | // NB: this triggers a different behaviour that piping multiple commands to stdin, namely |
||
| 89 | // it wraps all of the commands in a transaction and allows either sql commands or a single meta-command |
||
| 90 | if ($action == self::EXECUTE_COMMAND) { |
||
| 91 | $options[] = '--command=' . $sqlOrFilename; |
||
| 92 | } |
||
| 93 | //$env = [ |
||
| 94 | // problematic when wrapping the process in a call to `time`... |
||
| 95 | //'PGPASSWORD' => $this->databaseConfiguration['password'], |
||
| 96 | //]; |
||
| 97 | break; |
||
| 98 | |||
| 99 | case 'sqlcmd': |
||
| 100 | $command = 'sqlcmd'; |
||
| 101 | $options = [ |
||
| 102 | '-S' . $this->databaseConfiguration['host'] . ($this->databaseConfiguration['port'] != '' ? ',' . $this->databaseConfiguration['port'] : ''), |
||
| 103 | '-U' . $this->databaseConfiguration['user'], |
||
| 104 | '-P' . $this->databaseConfiguration['password'], |
||
| 105 | '-r1', |
||
| 106 | '-b', |
||
| 107 | ]; |
||
| 108 | if (isset($this->databaseConfiguration['dbname'])) { |
||
| 109 | $options[] = '-d' . $this->databaseConfiguration['dbname']; |
||
| 110 | } |
||
| 111 | if ($action == self::EXECUTE_FILE) { |
||
| 112 | $options[] = '-i' . $sqlOrFilename; |
||
| 113 | } elseif ($action == self::EXECUTE_COMMAND) { |
||
| 114 | $options[] = '-Q' . $sqlOrFilename; |
||
| 115 | } |
||
| 116 | break; |
||
| 117 | |||
| 118 | case 'sqlite': |
||
| 119 | $command = 'sqlite3'; |
||
| 120 | // 'path' is the full path to the 'master' db (for Doctrine compatibility). |
||
| 121 | // non-master dbs are supposed to reside in the same directory |
||
| 122 | if (isset($this->databaseConfiguration['dbname'])) { |
||
| 123 | $options[] = dirname($this->databaseConfiguration['path']) . '/' . $this->databaseConfiguration['dbname'] . '.sqlite'; |
||
| 124 | } else { |
||
| 125 | $options[] = $this->databaseConfiguration['path']; |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($action == self::EXECUTE_COMMAND) { |
||
| 129 | $options[] = $sqlOrFilename; |
||
| 130 | } |
||
| 131 | break; |
||
| 132 | |||
| 133 | // case 'sqlplus': |
||
| 134 | // $command = 'sqlplus'; |
||
| 135 | // // pass on _all_ env vars, including PATH |
||
| 136 | // $env = null; |
||
| 137 | // break; |
||
| 138 | default: |
||
| 139 | throw new \OutOfBoundsException("Unsupported db client '$clientType'"); |
||
| 140 | } |
||
| 141 | |||
| 142 | $commandLine = $this->buildCommandLine($command, $options); |
||
| 143 | |||
| 144 | /// @todo investigate: for psql is this better done via --file ? |
||
| 145 | if ($action == self::EXECUTE_FILE && $clientType != 'sqlsrv') { |
||
| 146 | $commandLine .= ' < ' . escapeshellarg($sqlOrFilename); |
||
| 147 | } |
||
| 148 | |||
| 149 | return new Process($commandLine, null, $env); |
||
| 150 | } |
||
| 217 |