Completed
Pull Request — master (#256)
by Anton
10:05
created

Row   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 11.46 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 61.76%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 11
loc 96
ccs 21
cts 34
cp 0.6176
rs 10
wmc 12
lcom 2
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeSave() 0 7 1
A createThumbnail() 0 13 1
B deleteFiles() 6 9 5
A beforeInsert() 5 18 3
A beforeUpdate() 0 4 1
A afterDelete() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Media;
11
12
use Application\Users;
13
use Bluz\Proxy\Auth;
14
use Bluz\Validator\Traits\Validator;
15
use Bluz\Validator\Validator as v;
16
use Image\Thumbnail;
17
18
/**
19
 * Media Row
20
 *
21
 * @category Application
22
 * @package  Media
23
 *
24
 * @property integer $id
25
 * @property integer $userId
26
 * @property string $title
27
 * @property string $module
28
 * @property string $type
29
 * @property string $file
30
 * @property string $thumb
31
 * @property integer $size
32
 * @property string $created
33
 * @property string $updated
34
 */
35
class Row extends \Bluz\Db\Row
36
{
37
    use Validator;
38
39
    const THUMB_HEIGHT = 196;
40
    const THUMB_WIDTH = 196;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    protected function beforeSave()
46
    {
47 1
        $this->addValidator(
48 1
            'title',
49 1
            v::required()->latinNumeric(' -_.')
50
        );
51 1
    }
52
53
    /**
54
     * Create thumbnail
55
     *
56
     * @return string
57
     */
58 1
    public function createThumbnail()
59
    {
60
        // set full path
61 1
        $image = new Thumbnail(PATH_PUBLIC .'/'. $this->file);
62 1
        $image->setHeight(self::THUMB_HEIGHT);
63 1
        $image->setWidth(self::THUMB_WIDTH);
64 1
        $thumb = $image->generate();
65
        // crop full path
66 1
        $thumb = substr($thumb, strlen(PATH_PUBLIC) + 1);
67 1
        $this->thumb = $thumb;
68
69 1
        return $thumb;
70
    }
71
72
    /**
73
     * Delete Files
74
     *
75
     * @return void
76
     */
77
    public function deleteFiles()
78
    {
79 View Code Duplication
        if ($this->file && is_file(PATH_PUBLIC .'/'. $this->file)) {
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...
80
            @unlink(PATH_PUBLIC .'/'. $this->file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
81
        }
82 View Code Duplication
        if ($this->thumb && is_file(PATH_PUBLIC .'/'. $this->thumb)) {
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...
83
            @unlink(PATH_PUBLIC .'/'. $this->thumb);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
84
        }
85
    }
86
87
    /**
88
     * __insert
89
     *
90
     * @return void
91
     */
92 1
    protected function beforeInsert()
93
    {
94 1
        $this->created = gmdate('Y-m-d H:i:s');
95
96
        // set default module
97 1
        if (!$this->module) {
98 1
            $this->module = 'users';
99
        }
100
        // set user ID
101 1 View Code Duplication
        if ($user = Auth::getIdentity()) {
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...
102 1
            $this->userId = $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Bluz\Auth\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
103
        } else {
104
            $this->userId = Users\Table::SYSTEM_USER;
105
        }
106
107
        // create thumbnail
108 1
        $this->createThumbnail();
109 1
    }
110
111
    /**
112
     * __update
113
     *
114
     * @return void
115
     */
116
    protected function beforeUpdate()
117
    {
118
        $this->updated = gmdate('Y-m-d H:i:s');
119
    }
120
121
    /**
122
     * postDelete
123
     *
124
     * @return void
125
     */
126
    protected function afterDelete()
127
    {
128
        $this->deleteFiles();
129
    }
130
}
131