Entity::save()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.9848
c 0
b 0
f 0
cc 5
nc 8
nop 2
1
<?php
2
3
namespace Faulancer\ORM;
4
5
use Faulancer\Controller\Controller;
6
use Faulancer\Exception\InvalidArgumentException;
7
use Faulancer\ServiceLocator\ServiceLocator;
8
use ORM\EntityManager;
9
use ORM\Exception\IncompletePrimaryKey;
10
use ORM\Exception\InvalidConfiguration;
11
use ORM\Exception\InvalidName;
12
use ORM\Exception\NoConnection;
13
use ORM\Exception\NoEntity;
14
use ORM\Exception\NoEntityManager;
15
use ORM\Exception\NotScalar;
16
use ORM\Exception\UnsupportedDriver;
17
18
/**
19
 * Class Entity
20
 *
21
 * @property int $id
22
 *
23
 * @category Faulancer\ORM
24
 * @package  Faulancer\ORM
25
 * @author   Florian Knapp <[email protected]>
26
 * @license  MIT License
27
 * @link     No link provided
28
 */
29
abstract class Entity extends \ORM\Entity
30
{
31
    /**
32
     * Return data as array
33
     *
34
     * @return array
35
     *
36
     * @codeCoverageIgnore
37
     */
38
    public function getDataAsArray()
39
    {
40
        return $this->getData();
41
    }
42
43
    /**
44
     * Save entity to database and add error handling for duplicate keys
45
     *
46
     * @param EntityManager $dbManager     The custom entity manager
47
     * @param bool          $redirectOnDup If redirect should be initiated
48
     *                                     on duplicate key error
49
     *
50
     * @return Entity|\ORM\Entity|bool
51
     *
52
     * @throws NoEntity
53
     * @throws NoConnection
54
     * @throws NotScalar
55
     * @throws UnsupportedDriver
56
     * @throws IncompletePrimaryKey
57
     * @throws InvalidConfiguration
58
     * @throws InvalidName
59
     * @throws NoEntityManager
60
     * @throws IncompletePrimaryKey
61
     * @throws InvalidArgumentException
62
     */
63
    public function save(EntityManager $dbManager = null, $redirectOnDup = false)
64
    {
65
        if ($dbManager !== null) {
66
            $this->setEntityManager($dbManager);
67
        }
68
69
        try {
70
71
            parent::save();
72
73
        } catch (\PDOException $e) {
74
75
            if ($e->getCode() === "23000") {
76
77
                if ($redirectOnDup) {
78
79
                    /** @var Controller $controller */
80
                    $controller = ServiceLocator::instance()->get(
81
                        Controller::class
82
                    );
83
84
                    $uri = $controller->getRequest()->getPath();
85
86
                    $controller->getSessionManager()->setFlashMessage(
0 ignored issues
show
Bug introduced by
The method setFlashMessage does only exist in Faulancer\Session\SessionManager, but not in Faulancer\ServiceLocator\ServiceInterface.

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...
87
                        'db.duplicate.key', 'Eintrag bereits vorhanden!'
88
                    );
89
90
                    $controller->redirect($uri);
91
92
                }
93
94
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method ORM\Entity::save of type ORM\Entity.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
95
96
            }
97
98
        }
99
100
        return $this;
101
    }
102
103
}