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.
Test Setup Failed
Pull Request — master (#25)
by
unknown
11:20
created

ModelWithObject::withTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 View Code Duplication
            if(isset($this->object->$column)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
            if($pk != $column && isset($this->object->$column)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    /**
86
     * set entity table name
87 3
     * @param string $tableName
88 3
     * @return $this
89 3
     */
90 3
    public function withTable($tableName)
91 3
    {
92
        $this->entity->setTable($tableName);
93
        return $this;
94
    }
95
96
    protected function getColumns()
97
    {
98
        $columns = [];
99
        foreach ($this->entity->getProperties() as $p){
100
            $columns[] = $p->name;
101
        }
102
        return $columns;
103
    }
104
105
    /**
106
     * @var object
107
     */
108
    protected $object;
109
110
    /**
111
     * @var ModelContainer
112
     */
113
    protected $entity;
114
    /**
115
     * @var DB
116
     */
117
    protected $db;
118
}