Gallery   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 136
Duplicated Lines 13.24 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 18
loc 136
ccs 50
cts 50
cp 1
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 2
A getCount() 8 16 2
A hasImages() 10 18 3
A getImages() 0 4 1
B find() 0 27 4

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
 * Created by PhpStorm.
4
 * User: myslyvyi
5
 * Date: 05.01.2016
6
 * Time: 15:25
7
 */
8
namespace samsoncms\api;
9
10
use samsonframework\orm\QueryInterface;
11
12
/***
13
 *  Gallery additional field for material.
14
 *  This class enables getting all information about additional fields gallery for specific material.
15
 *  @author [email protected]
16
 * @deprecated Use \samsoncms\api\generator\GalleryCollection instead
17
 */
18
class Gallery
19
{
20
    /** @var integer materialFieldId Table materialField identifier */
21
    protected $materialFieldId = null;
22
23
    /** @var QueryInterface Database query interface */
24
    protected $query;
25
26
    /**
27
     * Constructor Gallery.
28
     * This constructor finds identifier additional field gallery from
29
     * database record its material and field identifiers.
30
     *
31
     * @param QueryInterface $query Database query interface
32
     * @param integer $materialId material identifier
33
     * @param integer $fieldId field identifier
34
     */
35 3
    public function __construct(QueryInterface $query, $materialId, $fieldId)
36
    {
37
        /** @var object $materialField additional field value database record*/
38 3
        $materialField = null;
0 ignored issues
show
Unused Code introduced by
$materialField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
40
        //set query interface
41 3
        $this->query = $query;
42
43
        //Find additional field value database record by its material and field identifiers.
44 3
        $materialField = $this->query->entity(MaterialField::ENTITY)
45 3
            ->where(Material::F_PRIMARY, $materialId)
46 3
            ->where(Field::F_PRIMARY, $fieldId)
47 3
            ->where(Material::F_DELETION, 1)
48 3
            ->first();
49
50 3
        if ($materialField) {
51
            //Set materialFieldId
52 3
            $this->materialFieldId = $materialField->id;
53 3
        }
54
55 3
    }
56
57
    /**
58
     * Getting quantity images in additional field gallery
59
     *
60
     * @return integer $count
61
     */
62 1
    public function getCount()
63
    {
64
        /**@var integer $count quantity images in additional field gallery */
65 1
        $count = 0;
66
67 1 View Code Duplication
        if ($this->hasImages()) {
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...
68
            // Getting quantity images for gallery
69 1
            $count = $this->query
70 1
                ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
71 1
                ->where(Field::F_DELETION, 1)
72 1
                ->where(MaterialField::F_PRIMARY, $this->materialFieldId)
73 1
                ->count();
74 1
        }
75
76 1
        return $count;
77
    }
78
79
    /**
80
     * Check on empty gallery. If materialFieldId = null and quantity images not more 1 then material not has images.
81
     *
82
     * @return boolean
83
     **/
84 3
    public function hasImages()
85
    {
86
        /**@var $hasImages */
87 3
        $hasImages = false;
88
89 3 View Code Duplication
        if (isset($this->materialFieldId)) {
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...
90
            // Getting quantity images, if quantity more 0 then material has images
91 3
            if ($this->query
92 3
            ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
93 3
            ->where(Field::F_DELETION, 1)
94 3
            ->where(MaterialField::F_PRIMARY, $this->materialFieldId)
95 3
            ->count() > 0) {
96 3
                $hasImages = true;
97 3
            }
98 3
        }
99
100 3
        return $hasImages;
101
    }
102
103
    /**
104
     * Get collection of images for material by gallery additional field selector. If none is passed
105
     * all images from gallery table would be returned empty array.
106
     *
107
     * @param integer $currentPage current page with images. Min value = 1
108
     * @param integer $countView quantity view by page
109
     *
110
*@return array
111
     * @deprecated  Use find()
112
     */
113 2
    public function getImages($currentPage = null, $countView = 20)
114
    {
115 2
        return $this->find($currentPage, $countView);
116
    }
117
118
    /**
119
     * Perform SamsonCMS query and get entity gallery images.
120
     *
121
     * @param int $page Page number
122
     * @param int $size Page size
123
     *
124
     * @return GalleryField[] Collection of entity gallery images
125
     */
126 2
    public function find($page = null, $size = null)
127
    {
128
        /** @var GalleryField[] $images Get material images for this gallery */
129 2
        $images = array();
130
131
        /** @var QueryInterface $query Database query interface*/
132 2
        $query = null;
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
134 2
        if ($this->hasImages()) {
135
            // Select all images in DB by materialFieldId
136 2
            $query = $this->query
137 2
                ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
138 2
                ->where(Field::F_DELETION, 1)
139 2
                ->where(MaterialField::F_PRIMARY, $this->materialFieldId);
140
141
            // Add paging
142 2
            if (isset($page) && $page > 0) {
143
                //Set limit for query
144 1
                $query->limit(--$page * $size, $size);
145 1
            }
146
147
            // Execute query
148 2
            $images = $query->exec();
149 2
        }
150
151 2
        return $images;
152
    }
153
}
154