Completed
Pull Request — master (#39)
by
unknown
02:27
created

ArticleTableGateway::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
namespace Admin\Db;
4
5
use Admin\Model\Entity\ArticleEntity;
6
use Admin\Model\Storage\ArticleStorageInterface;
7
use Zend\Db\TableGateway\TableGateway;
8
use Zend\Db\Adapter\AdapterInterface;
9
use Zend\Db\ResultSet\ResultSetInterface;
10
11
class ArticleTableGateway extends TableGateway implements ArticleStorageInterface
12
{
13
    protected $columns = array(
14
//        'articleUuid' => 'article_uuid',
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
15
//        'title' => 'title',
16
//        'publishedAt' => 'published_at',
17
//        'createdAt' => 'created_at',
18
//        'slug' => 'slug',
19
//        'type' => 'type',
20
    );
21
22
    /**
23
     * @param AdapterInterface   $adapter
24
     * @param ResultSetInterface $resultSet
25
     */
26
    public function __construct(AdapterInterface $adapter, ResultSetInterface $resultSet)
27
    {
28
        parent::__construct('articles', $adapter, null, $resultSet);
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function fetchAll($params)
35
    {
36
        return $this->select($params);
37
38
//        $select = $this->getSql()->select();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
//
40
//        return $this->selectWith($select);
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function fetchOne($params)
47
    {
48
        $select = $this->getSql()->select();
49
        $select->where->equalTo('article_uuid', $params['article_uuid']);
50
        $select->limit(1);
51
52
        return $this->selectWith($select);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->selectWith($select); of type null|Zend\Db\ResultSet\ResultSetInterface adds the type Zend\Db\ResultSet\ResultSetInterface to the return on line 52 which is incompatible with the return type declared by the interface Admin\Model\Storage\Arti...rageInterface::fetchOne of type Admin\Model\Entity\ArticleEntity.
Loading history...
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function create(ArticleEntity $article)
59
    {
60
        return $this->insert($article->getArrayCopy());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->insert($article->getArrayCopy()); (integer) is incompatible with the return type declared by the interface Admin\Model\Storage\Arti...torageInterface::create of type boolean.

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...
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    public function update($article, $where = null, array $joins = null)
67
    {
68
        $updateData = $article->getArrayCopy();
69
70
        $update = $this->getSql()->update();
71
        $update->set($updateData);
72
        $update->where->equalTo('article_uuid', $article->getArticle_uuid());
73
74
        return $this->updateWith($update) > 0;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->updateWith($update) > 0; (boolean) is incompatible with the return type of the parent method Zend\Db\TableGateway\AbstractTableGateway::update of type integer.

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...
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function delete($params)
81
    {
82
        $delete = $this->getSql()->delete();
83
        $delete->where->equalTo('article_uuid', $params['article_uuid']);
84
85
        return $this->deleteWith($delete) > 0;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->deleteWith($delete) > 0; (boolean) is incompatible with the return type of the parent method Zend\Db\TableGateway\AbstractTableGateway::delete of type integer.

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...
86
    }
87
}