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.
Passed
Push — master ( dc2150...abbaf5 )
by cao
03:11
created

ModelWithObject   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 92.16%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 47
cts 51
cp 0.9216
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
B create() 0 18 5
C update() 0 22 8
A delete() 0 7 1
A getColumns() 0 8 2
1
<?php
2
3
namespace PhpBoot\ORM;
4
5
use Doctrine\Common\Cache\Cache;
6
use PhpBoot\DB\DB;
7
8
class ModelWithObject
9
{
10
    /**
11
     * ModelWithObject constructor.
12
     * @param DB $db
13
     * @param object $entity
14
     * @param Cache $cache
15
     */
16 4
    public function __construct(DB $db, $entity, Cache $cache)
0 ignored issues
show
Unused Code introduced by
The parameter $cache is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
    {
18 4
        is_object($entity) or \PhpBoot\abort(new \InvalidArgumentException('object required'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
19 4
        $entityName = get_class($entity);
20 4
        $this->db = $db;
21 4
        $builder = $db->getApp()->get(ModelContainerBuilder::class);
22 4
        $this->entity = $builder->build($entityName);
23 4
        $this->object = $entity;
24 4
    }
25
    /**
26
     * @return void
27
     */
28 1
    public function create()
29
    {
30 1
        $data = [];
31 1
        foreach ($this->getColumns() as $column){
32 1
            if(isset($this->object->$column)){
33 1
                if(is_array($this->object->$column) || is_object($this->object->$column)){
34
                    $data[$column] = json_encode($this->object->$column);
35
                }else{
36 1
                    $data[$column] = $this->object->$column;
37
                }
38
39 1
            }
40 1
        }
41 1
        $id = $this->db->insertInto($this->entity->getTable())
42 1
            ->values($data)
43 1
            ->exec()->lastInsertId();
44 1
        $this->object->{$this->entity->getPK()} = $id;
45 1
    }
46
47
    /**
48
     * @param array $columns columns to update. if columns is empty array, update all of the columns
49
     * @return int rows updated
50
     */
51 2
    public function update(array $columns=[])
52
    {
53 2
        $data = [];
54 2
        $pk = $this->entity->getPK();
55 2
        foreach ($this->getColumns() as $column){
56 2
            if(count($columns) && !in_array($column, $columns)){
57 1
                continue;
58
            }
59 2
            if($pk != $column && isset($this->object->$column)){
60 2
                if(is_array($this->object->$column) || is_object($this->object->$column)){
61
                    $data[$column] = json_encode($this->object->$column);
62
                }else{
63 2
                    $data[$column] = $this->object->$column;
64
                }
65 2
            }
66 2
        }
67
68 2
        return $this->db->update($this->entity->getTable())
69 2
            ->set($data)
70 2
            ->where("`{$pk}` = ?", $this->object->$pk)
71 2
            ->exec()->rows;
72
    }
73
74
    /**
75
     * @return int rows deleted
76
     */
77 1
    public function delete()
78
    {
79 1
        $pk = $this->entity->getPK();
80 1
        return $this->db->deleteFrom($this->entity->getTable())
81 1
            ->where([$pk => $this->object->$pk])
82 1
            ->exec()->rows;
83
    }
84
85 3
    protected function getColumns()
86
    {
87 3
        $columns = [];
88 3
        foreach ($this->entity->getProperties() as $p){
89 3
            $columns[] = $p->name;
90 3
        }
91 3
        return $columns;
92
    }
93
94
    /**
95
     * @var object
96
     */
97
    protected $object;
98
99
    /**
100
     * @var ModelContainer
101
     */
102
    protected $entity;
103
    /**
104
     * @var DB
105
     */
106
    protected $db;
107
}