Completed
Pull Request — master (#1559)
by Grégoire
02:43
created

BaseGallery::reorderGalleryItems()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.3888
c 0
b 0
f 0
cc 5
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\PHPCR;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Sonata\MediaBundle\Model\Gallery;
18
use Sonata\MediaBundle\Model\GalleryItemInterface;
19
20
/**
21
 * Bundle\MediaBundle\Document\BaseGallery.
22
 */
23
abstract class BaseGallery extends Gallery
24
{
25
    /**
26
     * @var string
27
     */
28
    private $uuid;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function __construct()
34
    {
35
        $this->galleryItems = new ArrayCollection();
36
    }
37
38
    /**
39
     * Get universal unique id.
40
     *
41
     * @return string
42
     */
43
    public function getUuid()
44
    {
45
        return $this->uuid;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function addGalleryItem(GalleryItemInterface $galleryItem): void
52
    {
53
        $galleryItem->setGallery($this);
54
55
        // set nodename of GalleryItem
56
        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...
57
            $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...
58
                'media'.($this->galleryItems->count() + 1)
59
            );
60
        }
61
62
        $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...
63
    }
64
65
    /**
66
     * Pre persist method.
67
     */
68
    public function prePersist(): void
69
    {
70
        $this->createdAt = new \DateTime();
71
        $this->updatedAt = new \DateTime();
72
73
        $this->reorderGalleryItems();
0 ignored issues
show
Bug introduced by
The method reorderGalleryItems() does not seem to exist on object<Sonata\MediaBundle\PHPCR\BaseGallery>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
    }
75
76
    /**
77
     * Pre Update method.
78
     */
79
    public function preUpdate(): void
80
    {
81
        $this->updatedAt = new \DateTime();
82
83
        $this->reorderGalleryItems();
0 ignored issues
show
Bug introduced by
The method reorderGalleryItems() does not seem to exist on object<Sonata\MediaBundle\PHPCR\BaseGallery>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
    }
85
}
86