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

src/Gallery.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type samsoncms\api\Gallery has been defined more than once; this definition is ignored, only the first definition in Gallery.php (L19-134) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
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 View Code Duplication
    public function __construct(QueryInterface $query, $materialId, $fieldId)
0 ignored issues
show
This method seems to be duplicated in 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...
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
        if (isset($this->materialFieldId)) {
62
            // Getting quantity images, if quantity more 0 then material has images
63
            if ($this->query
0 ignored issues
show
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 View Code Duplication
    public function getImages()
0 ignored issues
show
This method seems to be duplicated in 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...
81
    {
82
        /** @var $images[] Get material images for this gallery */
83
        $images = array();
84
85
        if ($this->hasImages()) {
86
            //Get All images for materialFieldId
87
            $images = $this->query
0 ignored issues
show
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
}