Completed
Branch 7-dev (bf2895)
by Oscar
03:53
created

Common   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 47
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A getTable() 0 4 1
A __call() 0 10 2
A __toString() 0 4 1
A getValues() 0 5 1
A run() 0 4 1
A __invoke() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Query\Traits;
5
6
use BadMethodCallException;
7
use PDOStatement;
8
use SimpleCrud\Query\QueryInterface;
9
use SimpleCrud\Table;
10
11
trait Common
12
{
13
    private $table;
14
    private $query;
15
16
    public static function create(Table $table, array $arguments): QueryInterface
17
    {
18
        return new static($table, ...$arguments);
0 ignored issues
show
Unused Code introduced by
The call to Common::__construct() has too many arguments starting with $table.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
19
    }
20
21
    public function getTable(): Table
22
    {
23
        return $this->table;
24
    }
25
26
    public function __call(string $name, array $arguments)
27
    {
28
        if (!in_array($name, $this->allowedMethods)) {
0 ignored issues
show
Bug introduced by
The property allowedMethods does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
            throw new BadMethodCallException(sprintf('The method "%s" is not valid', $name));
30
        }
31
32
        $this->query->{$name}(...$arguments);
33
34
        return $this;
35
    }
36
37
    public function __toString()
38
    {
39
        return $this->query->getStatement();
40
    }
41
42
    public function getValues(): array
43
    {
44
        $values = $this->query->getBindValues();
45
        return array_column($values, 0);
46
    }
47
48
    public function run()
49
    {
50
        $this->__invoke();
51
    }
52
53
    public function __invoke(): PDOStatement
54
    {
55
        return $this->query->perform();
56
    }
57
}
58