Completed
Branch master (b4ab83)
by Mikołaj
03:16
created

AddModel::add()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 3
nop 1
dl 0
loc 49
rs 8.5906
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Modules\Galleries\One\Admin;
4
5
use Rudolf\Framework\Model\AdminModel;
6
use Rudolf\Component\Modules\Module;
7
8
class AddModel extends AdminModel
9
{
10
    /**
11
     * Add article.
12
     *
13
     * @param array $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
14
     *
15
     * @return int
16
     */
17
    public function add($f)
18
    {
19
        $userInfo = self::$auth->getUser();
20
        $f['added'] = date('Y-m-d H:i:s');
21
        $f['adder_ID'] = $userInfo['id'];
22
23
        $stmt = $this->pdo->prepare("
24
            INSERT INTO {$this->prefix}galleries
25
                (title
26
                , added
27
                , adder_ID
28
                , slug
29
                , thumb_width
30
                , thumb_height)
31
            VALUES
32
                (:title
33
                , :added
34
                , :adder_ID
35
                , :slug
36
                , :thumb_width
37
                , :thumb_height)
38
        ");
39
        $stmt->bindValue(':title', $f['title'], \PDO::PARAM_STR);
40
        $stmt->bindValue(':added', $f['added'], \PDO::PARAM_STR);
41
        $stmt->bindValue(':adder_ID', $f['adder_ID'], \PDO::PARAM_INT);
42
        $stmt->bindValue(':slug', $f['slug'], \PDO::PARAM_STR);
43
        $stmt->bindValue(':thumb_width', $f['thumb_width'] ? $f['thumb_width'] : 209, \PDO::PARAM_INT);
44
        $stmt->bindValue(':thumb_height', $f['thumb_height'] ? $f['thumb_height'] : 157, \PDO::PARAM_INT);
45
46
        if (!$stmt->execute()) {
47
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Rudolf\Modules\Galleries\One\Admin\AddModel::add 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...
48
        }
49
50
        $id = $this->pdo->lastInsertId();
51
        $config = (new Module('galleries'))->getConfig();
52
        $directory = $config['path_root'].'/'.$f['slug'];
53
54
        if (file_exists($directory)) {
55
            $directory = $directory.'-'.$id;
56
            $stmt = $this->pdo->prepare("UPDATE {$this->prefix}galleries SET slug = :slug WHERE id = :id");
57
            $stmt->bindValue(':slug', $f['slug'].'-'.$id, \PDO::PARAM_STR);
58
            $stmt->bindValue(':id', $id, \PDO::PARAM_INT);
59
            $stmt->execute();
60
        }
61
62
        mkdir($directory);
63
64
        return $id;
65
    }
66
}
67