GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ecf3ef...78a151 )
by Robert
11:40
created

Command::extractUsedParams()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.2
c 1
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
crap 4
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 205
    public function execute()
27
    {
28 205
        $sql = $this->getSql();
29 205
        $params = $this->params;
30 205
        $statements = $this->splitStatements($sql, $params);
31 205
        if ($statements === false) {
32 200
            return parent::execute();
33
        }
34
35 10
        $result = null;
36 10
        foreach ($statements as $statement) {
37 10
            list($statementSql, $statementParams) = $statement;
38 10
            $this->setSql($statementSql)->bindValues($statementParams);
39 10
            $result = parent::execute();
40
        }
41 10
        $this->setSql($sql)->bindValues($params);
42 10
        return $result;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 395
    protected function queryInternal($method, $fetchMode = null)
49
    {
50 395
        $sql = $this->getSql();
51 395
        $params = $this->params;
52 395
        $statements = $this->splitStatements($sql, $params);
53 395
        if ($statements === false) {
54 395
            return parent::queryInternal($method, $fetchMode);
55
        }
56
57 1
        list($lastStatementSql, $lastStatementParams) = array_pop($statements);
58 1
        foreach ($statements as $statement) {
59 1
            list($statementSql, $statementParams) = $statement;
60 1
            $this->setSql($statementSql)->bindValues($statementParams);
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);
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 412
    private function splitStatements($sql, $params)
77
    {
78 412
        $semicolonIndex = strpos($sql, ';');
79 412
        if ($semicolonIndex === false || $semicolonIndex === StringHelper::byteLength($sql) - 1) {
80 412
            return false;
81
        }
82
83 10
        $tokenizer = new SqlTokenizer($sql);
84 10
        $codeToken = $tokenizer->tokenize();
85 10
        if (count($codeToken->getChildren()) === 1) {
86
            return false;
87
        }
88
89 10
        $statements = [];
90 10
        foreach ($codeToken->getChildren() as $statement) {
91 10
            $statements[] = [$statement->getSql(), $this->extractUsedParams($statement, $params)];
92
        }
93 10
        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 10
    private function extractUsedParams(SqlToken $statement, $params)
103
    {
104 10
        preg_match_all('/(?P<placeholder>[:][a-zA-Z0-9_]+)/', $statement->getSql(), $matches, PREG_SET_ORDER);
105 10
        $result = [];
106 10
        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 10
            $phName = ltrim($match['placeholder'], ':');
108 10
            if (isset($params[$phName])) {
109 1
                $result[$phName] = $params[$phName];
110 9
            } elseif (isset($params[':' . $phName])) {
111 10
                $result[':' . $phName] = $params[':' . $phName];
112
            }
113
        }
114 10
        return $result;
115
    }
116
}
117