Completed
Push — master ( 7c9586...c8ce39 )
by Oscar
01:44
created

Insert   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 48.65 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 18
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 18 18 3
A run() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Query;
5
6
use SimpleCrud\Events\CreateInsertQuery;
7
use SimpleCrud\Table;
8
9
final class Insert implements QueryInterface
10
{
11
    use Traits\Common;
12
13
    private $allowedMethods = [
14
        'setFlag',
15
        'set',
16
    ];
17
18 View Code Duplication
    public function __construct(Table $table, array $data = [])
19
    {
20
        $this->table = $table;
21
22
        $this->query = $table->getDatabase()
23
            ->insert()
24
            ->into((string) $table);
25
26
        foreach ($data as $fieldName => $value) {
27
            $this->table->{$fieldName}->insert($this->query, $value);
28
        }
29
30
        $eventDispatcher = $table->getEventDispatcher();
31
32
        if ($eventDispatcher) {
33
            $eventDispatcher->dispatch(new CreateInsertQuery($this));
0 ignored issues
show
Documentation introduced by
new \SimpleCrud\Events\CreateInsertQuery($this) is of type object<SimpleCrud\Events\CreateInsertQuery>, but the function expects a object<Psr\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
        }
35
    }
36
37
    public function run()
38
    {
39
        $this->__invoke();
40
41
        $id = $this->table->getDatabase()->lastInsertId();
42
43
        return $this->table->id->format($id);
44
    }
45
}
46