Model::delete()   C
last analyzed

Complexity

Conditions 7
Paths 22

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 19
cts 19
cp 1
rs 6.7272
cc 7
eloc 17
nc 22
nop 1
crap 7
1
<?php
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Model;
13
14
use Rocket\ORM\Connection\ConnectionInterface;
15
use Rocket\ORM\Model\Map\TableMapInterface;
16
use Rocket\ORM\Rocket;
17
18
/**
19
 * @author Sylvain Lorinet <[email protected]>
20
 */
21
abstract class Model implements ModelInterface
22
{
23
    /**
24
     * @var bool
25
     */
26
    protected $_isNew = true;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $_isModified = false;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $_isDeleted = false;
37
38
    /**
39
     * @var TableMapInterface
40
     */
41
    protected $tableMap;
42
43
44
    /**
45
     * @param \PDO $con
0 ignored issues
show
Documentation introduced by
Should the type for parameter $con not be null|\PDO?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
46
     *
47
     * @return bool
48
     *
49
     * @throws \Exception
50
     */
51 7
    public function save(\PDO $con = null)
52
    {
53 7
        if ($this->_isDeleted) {
54 1
            throw new \LogicException('Cannot save a deleted object');
55
        }
56
57 6
        if (null == $con) {
58 5
            $con = Rocket::getConnection($this->getTableMap()->getConnectionName(), ConnectionInterface::CONNECTION_MODE_WRITE);
59 5
        }
60
61
        try {
62 6
            if ($this->preSave($con)) {
0 ignored issues
show
Bug introduced by
It seems like $con can also be of type object<Rocket\ORM\Connection\ConnectionInterface>; however, Rocket\ORM\Model\Model::preSave() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
63 6
                if ($this->_isNew) {
64 5
                    $this->doInsert($con);
0 ignored issues
show
Bug introduced by
It seems like $con can also be of type object<Rocket\ORM\Connection\ConnectionInterface>; however, Rocket\ORM\Model\Model::doInsert() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65 3
                    $this->postSave($con);
0 ignored issues
show
Bug introduced by
It seems like $con can also be of type object<Rocket\ORM\Connection\ConnectionInterface>; however, Rocket\ORM\Model\Model::postSave() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Unused Code introduced by
The call to the method Rocket\ORM\Model\Model::postSave() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
66 4
                } elseif ($this->_isModified) {
67 1
                    if ($this->_isModified) {
68 1
                        $this->doUpdate($con);
0 ignored issues
show
Bug introduced by
It seems like $con can also be of type object<Rocket\ORM\Connection\ConnectionInterface>; however, Rocket\ORM\Model\Model::doUpdate() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69 1
                        $this->postSave($con);
0 ignored issues
show
Bug introduced by
It seems like $con can also be of type object<Rocket\ORM\Connection\ConnectionInterface>; however, Rocket\ORM\Model\Model::postSave() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Unused Code introduced by
The call to the method Rocket\ORM\Model\Model::postSave() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
70 1
                    }
71 1
                }
72 4
            }
73 6
        } catch (\Exception $e) {
74 2
            if ($con->inTransaction()) {
0 ignored issues
show
Bug introduced by
The method inTransaction does only exist in PDO, but not in Rocket\ORM\Connection\ConnectionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
75 1
                $con->rollBack();
0 ignored issues
show
Bug introduced by
The method rollBack does only exist in PDO, but not in Rocket\ORM\Connection\ConnectionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76 1
            }
77
78 2
            throw $e;
79
        }
80
81 4
        $this->_isNew = false;
82 4
        $this->_isModified = false;
83
84 4
        return true;
85
    }
86
87
    /**
88
     * @param \PDO $con
0 ignored issues
show
Documentation introduced by
Should the type for parameter $con not be null|\PDO?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
89
     *
90
     * @return bool
91
     *
92
     * @throws \Exception
93
     */
94 4
    public function delete(\PDO $con = null)
95
    {
96 4
        if ($this->_isNew) {
97 1
            throw new \LogicException('Cannot delete a new object');
98
        }
99
100 3
        if ($this->_isDeleted) {
101 1
            throw new \LogicException('Cannot delete an already deleted object');
102
        }
103
104 2
        if (null == $con) {
105 1
            $con = Rocket::getConnection($this->getTableMap()->getConnectionName(), ConnectionInterface::CONNECTION_MODE_WRITE);
106 1
        }
107
        try {
108 2
            if ($this->preDelete($con)) {
0 ignored issues
show
Bug introduced by
It seems like $con can also be of type object<Rocket\ORM\Connection\ConnectionInterface>; however, Rocket\ORM\Model\Model::preDelete() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
109 2
                $this->doDelete($con);
0 ignored issues
show
Bug introduced by
It seems like $con can also be of type object<Rocket\ORM\Connection\ConnectionInterface>; however, Rocket\ORM\Model\Model::doDelete() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
110 1
            }
111
112 1
            $this->postDelete($con);
0 ignored issues
show
Bug introduced by
It seems like $con can also be of type object<Rocket\ORM\Connection\ConnectionInterface>; however, Rocket\ORM\Model\Model::postDelete() does only seem to accept object<PDO>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Unused Code introduced by
The call to the method Rocket\ORM\Model\Model::postDelete() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
113 2
        } catch (\Exception $e) {
114 1
            if ($con->inTransaction()) {
0 ignored issues
show
Bug introduced by
The method inTransaction does only exist in PDO, but not in Rocket\ORM\Connection\ConnectionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
115 1
                $con->rollBack();
0 ignored issues
show
Bug introduced by
The method rollBack does only exist in PDO, but not in Rocket\ORM\Connection\ConnectionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
116 1
            }
117
118 1
            throw $e;
119
        }
120
121 1
        $this->_isDeleted = true;
122
123 1
        return true;
124
    }
125
126
    /**
127
     * @param \PDO $con
128
     *
129
     * @return bool
130
     */
131 7
    protected function preSave(\PDO $con)
0 ignored issues
show
Unused Code introduced by
The parameter $con 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...
132
    {
133 7
        return true;
134
    }
135
136
    /**
137
     * @param \PDO $con
138
     *
139
     * @return bool
140
     */
141 5
    protected function postSave(\PDO $con)
0 ignored issues
show
Unused Code introduced by
The parameter $con 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...
142
    {
143 5
        return true;
144
    }
145
146
    /**
147
     * @param \PDO $con
148
     *
149
     * @return bool
150
     */
151 3
    protected function preDelete(\PDO $con)
0 ignored issues
show
Unused Code introduced by
The parameter $con 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...
152
    {
153 3
        return true;
154
    }
155
156
    /**
157
     * @param \PDO $con
158
     *
159
     * @return bool
160
     */
161 2
    protected function postDelete(\PDO $con)
0 ignored issues
show
Unused Code introduced by
The parameter $con 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...
162
    {
163 2
        return true;
164
    }
165
166
    /**
167
     * @return TableMapInterface
168
     */
169 4
    protected function getTableMap()
170
    {
171 4
        if (!isset($this->tableMap)) {
172 2
            $this->tableMap = Rocket::getTableMap(get_called_class());
173 2
        }
174
175 4
        return $this->tableMap;
176
    }
177
178
    /**
179
     * @param \PDO $con
180
     *
181
     * @return void
182
     */
183
    protected abstract function doInsert(\PDO $con);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
184
185
    /**
186
     * @param \PDO $con
187
     *
188
     * @return void
189
     */
190
    protected abstract function doUpdate(\PDO $con);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
191
192
    /**
193
     * @param \PDO $con
194
     *
195
     * @return void
196
     */
197
    protected abstract function doDelete(\PDO $con);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
198
}
199