Completed
Push — master ( 86bee8...f7a629 )
by Grégoire
04:38
created

BaseGallery::getUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\PHPCR;
13
14
use Sonata\MediaBundle\Model\Gallery;
15
use Sonata\MediaBundle\Model\GalleryItemInterface;
16
17
/**
18
 * Bundle\MediaBundle\Document\BaseGallery.
19
 */
20
abstract class BaseGallery extends Gallery
21
{
22
    /**
23
     * @var string
24
     */
25
    private $uuid;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __construct()
31
    {
32
        $this->galleryItems = new \Doctrine\Common\Collections\ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<Son...\GalleryItemInterface>> of property $galleryItems.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    /**
36
     * Get universal unique id.
37
     *
38
     * @return string
39
     */
40
    public function getUuid()
41
    {
42
        return $this->uuid;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function addGalleryItem(GalleryItemInterface $galleryItem)
49
    {
50
        $galleryItem->setGallery($this);
51
52
        // set nodename of GalleryItem
53
        if (!$galleryItem->getNodename()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sonata\MediaBundle\Model\GalleryItemInterface as the method getNodename() does only exist in the following implementations of said interface: Sonata\MediaBundle\PHPCR\BaseGalleryItem.

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...
54
            $galleryItem->setNodename(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sonata\MediaBundle\Model\GalleryItemInterface as the method setNodename() does only exist in the following implementations of said interface: Sonata\MediaBundle\PHPCR\BaseGalleryItem.

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...
55
                'media'.($this->galleryItems->count() + 1)
0 ignored issues
show
Bug introduced by
The method count cannot be called on $this->galleryItems (of type array<integer,object<Son...\GalleryItemInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
56
            );
57
        }
58
59
        $this->galleryItems->set($galleryItem->getNodename(), $galleryItem);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sonata\MediaBundle\Model\GalleryItemInterface as the method getNodename() does only exist in the following implementations of said interface: Sonata\MediaBundle\PHPCR\BaseGalleryItem.

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...
Bug introduced by
The method set cannot be called on $this->galleryItems (of type array<integer,object<Son...\GalleryItemInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
60
    }
61
62
    /**
63
     * Pre persist method.
64
     */
65
    public function prePersist()
66
    {
67
        $this->createdAt = new \DateTime();
68
        $this->updatedAt = new \DateTime();
69
70
        $this->reorderGalleryItems();
71
    }
72
73
    /**
74
     * Pre Update method.
75
     */
76
    public function preUpdate()
77
    {
78
        $this->updatedAt = new \DateTime();
79
80
        $this->reorderGalleryItems();
81
    }
82
83
    /**
84
     * Reorders gallery items based on their position.
85
     */
86
    public function reorderGalleryItems()
87
    {
88
        if ($this->getGalleryItems() && $this->getGalleryItems() instanceof \IteratorAggregate) {
89
90
            // reorder
91
            $iterator = $this->getGalleryItems()->getIterator();
92
93
            $iterator->uasort(function ($a, $b) {
94
                if ($a->getPosition() === $b->getPosition()) {
95
                    return 0;
96
                }
97
98
                return $a->getPosition() > $b->getPosition() ? 1 : -1;
99
            });
100
101
            $this->setGalleryItems($iterator);
102
        }
103
    }
104
}
105