Completed
Pull Request — master (#6)
by Dima
14:07
created

Gallery   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 22.78 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 9
c 4
b 1
f 1
lcom 1
cbo 2
dl 18
loc 79
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
A hasImages() 10 14 3
A getImages() 8 16 2

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 samson\cms\CMSGallery;
11
use samsoncms\api\MaterialField;
12
use samsonframework\orm\QueryInterface;
13
14
/***
15
 *  Gallery additional field for material.
16
 *  This class enables getting all information about additional fields gallery for specific material.
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 database record its material and field identifiers.
29
     *
30
     * @param QueryInterface $query Database query interface
31
     * @param integer $materialId material identifier
32
     * @param integer $fieldId field identifier
33
     */
34
    public function __construct(QueryInterface $query, $materialId, $fieldId)
35
    {
36
        /** @var object $materialField additional field value database record*/
37
        $materialField = null;
38
39
        //set query interface
40
        $this->query = $query;
41
42
        // Checking params by type
43
        if (is_int($materialId) && is_int($fieldId)) {
44
            //Find additional field value database record by its material and field identifiers.
45
            if (MaterialField::byFieldIDAndMaterialID($query, $materialId, $fieldId, $materialField)) {
46
                //Getting first record
47
                $materialField = array_shift($materialField);
48
                //Set materialFieldId
49
                $this->materialFieldId = $materialField->id;
50
            }
51
        }
52
    }
53
54
    /**
55
     * Check on empty gallery. If materialFieldId = 0 and quantity images not more 1 then material not has images.
56
     *
57
     * @return boolean
58
     **/
59
    public function hasImages()
60
    {
61 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...
62
            // Getting quantity images, if quantity more 0 then material has images
63
            if ($this->query
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method cond() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
64
            ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
65
            ->cond(Field::F_DELETION, 1)
66
            ->where(MaterialField::F_PRIMARY, $this->materialFieldId)
67
            ->count() > 0) {
68
                return true;
69
            }
70
        }
71
        return false;
72
    }
73
74
    /**
75
     * Get collection of images for material by gallery additional field selector. If none is passed
76
     * all images from gallery table would be returned empty array.
77
     *
78
     * @return array
79
     */
80
    public function getImages()
81
    {
82
        /** @var $images[] Get material images for this gallery */
83
        $images = array();
84
85 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...
86
            //Get All images for materialFieldId
87
            $images = $this->query
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface samsonframework\orm\QueryInterface as the method cond() does only exist in the following implementations of said interface: samson\activerecord\dbQuery.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
88
                ->entity(CMS::MATERIAL_IMAGES_RELATION_ENTITY)
89
                ->cond(Field::F_DELETION, 1)
90
                ->where(MaterialField::F_PRIMARY, $this->materialFieldId)
91
                ->exec();
92
        }
93
94
        return $images;
95
    }
96
}