Completed
Push — 2.1 ( d5f162...fcc12f )
by
unknown
49:41 queued 41:18
created

Command::splitStatements()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 12
nc 4
nop 2
crap 5.0144
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db\sqlite;
9
10
use yii\db\SqlToken;
11
use yii\helpers\StringHelper;
12
13
/**
14
 * Command represents an SQLite's SQL statement to be executed against a database.
15
 *
16
 * {@inheritdoc}
17
 *
18
 * @author Sergey Makinen <[email protected]>
19
 * @since 2.0.14
20
 */
21
class Command extends \yii\db\Command
22
{
23
    /**
24
     * @inheritdoc
25
     */
26 192
    public function execute()
27
    {
28 192
        $sql = $this->getSql();
29 192
        $params = $this->params;
30 192
        $statements = $this->splitStatements($sql, $params);
31 192
        if ($statements === false) {
32 191
            return parent::execute();
33
        }
34
35 1
        $result = null;
36 1
        foreach ($statements as $statement) {
37 1
            [$statementSql, $statementParams] = $statement;
0 ignored issues
show
Bug introduced by
The variable $statementSql does not exist. Did you mean $statements?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $statementParams does not exist. Did you mean $params?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
38 1
            $this->setSql($statementSql)->bindValues($statementParams);
0 ignored issues
show
Bug introduced by
The variable $statementSql does not exist. Did you mean $statements?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $statementParams does not exist. Did you mean $params?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
39 1
            $result = parent::execute();
40
        }
41 1
        $this->setSql($sql)->bindValues($params);
42 1
        return $result;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 368
    protected function queryInternal($method, $fetchMode = null)
49
    {
50 368
        $sql = $this->getSql();
51 368
        $params = $this->params;
52 368
        $statements = $this->splitStatements($sql, $params);
53 368
        if ($statements === false) {
54 368
            return parent::queryInternal($method, $fetchMode);
55
        }
56
57 1
        [$lastStatementSql, $lastStatementParams] = array_pop($statements);
0 ignored issues
show
Bug introduced by
The variable $lastStatementSql does not exist. Did you mean $statements?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $lastStatementParams does not exist. Did you mean $params?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
58 1
        foreach ($statements as $statement) {
59 1
            [$statementSql, $statementParams] = $statement;
0 ignored issues
show
Bug introduced by
The variable $statementSql does not exist. Did you mean $statements?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $statementParams does not exist. Did you mean $params?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
60 1
            $this->setSql($statementSql)->bindValues($statementParams);
0 ignored issues
show
Bug introduced by
The variable $statementSql does not exist. Did you mean $statements?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $statementParams does not exist. Did you mean $params?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
61 1
            parent::execute();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (execute() instead of queryInternal()). Are you sure this is correct? If so, you might want to change this to $this->execute().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
62
        }
63 1
        $this->setSql($lastStatementSql)->bindValues($lastStatementParams);
0 ignored issues
show
Bug introduced by
The variable $lastStatementSql does not exist. Did you mean $statements?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $lastStatementParams does not exist. Did you mean $params?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
64 1
        $result = parent::queryInternal($method, $fetchMode);
65 1
        $this->setSql($sql)->bindValues($params);
66 1
        return $result;
67
    }
68
69
    /**
70
     * Splits the specified SQL code into individual SQL statements and returns them
71
     * or `false` if there's a single statement.
72
     * @param string $sql
73
     * @param array $params
74
     * @return string[]|false
75
     */
76 385
    private function splitStatements($sql, $params)
77
    {
78 385
        $semicolonIndex = strpos($sql, ';');
79 385
        if ($semicolonIndex === false || $semicolonIndex === StringHelper::byteLength($sql) - 1) {
80 385
            return false;
81
        }
82
83 1
        $tokenizer = new SqlTokenizer($sql);
84 1
        $codeToken = $tokenizer->tokenize();
85 1
        if (count($codeToken->getChildren()) === 1) {
86
            return false;
87
        }
88
89 1
        $statements = [];
90 1
        foreach ($codeToken->getChildren() as $statement) {
91 1
            $statements[] = [$statement->getSql(), $this->extractUsedParams($statement, $params)];
92
        }
93 1
        return $statements;
94
    }
95
96
    /**
97
     * Returns named bindings used in the specified statement token.
98
     * @param SqlToken $statement
99
     * @param array $params
100
     * @return array
101
     */
102 1
    private function extractUsedParams(SqlToken $statement, $params)
103
    {
104 1
        preg_match_all('/(?P<placeholder>[:][a-zA-Z0-9_]+)/', $statement->getSql(), $matches, PREG_SET_ORDER);
105 1
        $result = [];
106 1
        foreach ($matches as $match) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
107 1
            $phName = ltrim($match['placeholder'], ':');
108 1
            if (isset($params[$phName])) {
109 1
                $result[$phName] = $params[$phName];
110
            } elseif (isset($params[':' . $phName])) {
111 1
                $result[':' . $phName] = $params[':' . $phName];
112
            }
113
        }
114 1
        return $result;
115
    }
116
}
117