Completed
Push — translation-class ( c4acc3 )
by Kamil
18:30
created

Book::createTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 AppBundle\Entity;
13
14
use Sylius\Component\Resource\Model\ResourceInterface;
15
use Sylius\Component\Resource\Model\TranslatableInterface;
16
use Sylius\Component\Resource\Model\TranslatableTrait;
17
18
/**
19
 * @author Anna Walasek <[email protected]>
20
 */
21
class Book implements ResourceInterface, TranslatableInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addTranslation, getTranslation, getTranslations, hasTranslation, removeTranslation, setCurrentLocale, setFallbackLocale
Loading history...
22
{
23
    use TranslatableTrait {
24
        __construct as private initializeTranslationsCollection;
25
    }
26
27
    /**
28
     * @var int
29
     */
30
    private $id;
31
32
    /**
33
     * @var string
34
     */
35
    private $author;
36
37
    /**
38
     * @return int
39
     */
40
    public function getId()
41
    {
42
        return $this->id;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getTitle()
49
    {
50
        return $this->getTranslation()->getTitle();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method getTitle() does only exist in the following implementations of said interface: AppBundle\Entity\BookTranslation.

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...
51
    }
52
53
    /**
54
     * @param string $title
55
     */
56
    public function setTitle($title)
57
    {
58
        $this->getTranslation()->setTitle($title);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method setTitle() does only exist in the following implementations of said interface: AppBundle\Entity\BookTranslation.

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...
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getAuthor()
65
    {
66
        return $this->author;
67
    }
68
69
    /**
70
     * @param string $author
71
     */
72
    public function setAuthor($author)
73
    {
74
        $this->author = $author;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    protected function createTranslation()
81
    {
82
        return new BookTranslation();
83
    }
84
}
85