Filler::getForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Filler;
11
12
use AnimeDb\Bundle\CatalogBundle\Plugin\Plugin;
13
use Knp\Menu\ItemInterface;
14
use AnimeDb\Bundle\CatalogBundle\Entity\Item;
15
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Filler as FillerForm;
16
use Symfony\Bundle\FrameworkBundle\Routing\Router;
17
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Item as ItemSearch;
18
19
/**
20
 * Plugin filler.
21
 *
22
 * @author  Peter Gribanov <[email protected]>
23
 */
24
abstract class Filler extends Plugin implements FillerInterface
0 ignored issues
show
Deprecated Code introduced by
The class AnimeDb\Bundle\CatalogBundle\Plugin\Plugin has been deprecated with message: use PluginInterface

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
25
{
26
    /**
27
     * @var Router
28
     */
29
    protected $router;
30
31
    /**
32
     * @param ItemInterface $item
33
     *
34
     * @return ItemInterface
35
     */
36 1 View Code Duplication
    public function buildMenu(ItemInterface $item)
0 ignored issues
show
Duplication introduced by
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...
37
    {
38 1
        return $item->addChild($this->getTitle(), [
39 1
            'route' => 'fill_filler',
40 1
            'routeParameters' => ['plugin' => $this->getName()],
41 1
        ]);
42
    }
43
44
    /**
45
     * @return Filler
46
     */
47 6
    public function getForm()
48
    {
49 6
        return new FillerForm();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \AnimeDb\Bund...m\Type\Plugin\Filler(); (AnimeDb\Bundle\CatalogBu...Form\Type\Plugin\Filler) is incompatible with the return type declared by the interface AnimeDb\Bundle\CatalogBu...illerInterface::getForm of type AnimeDb\Bundle\CatalogBu...ugin\Fill\Filler\Filler.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
50
    }
51
52
    /**
53
     * @param Router $router
54
     */
55 1
    public function setRouter(Router $router)
56
    {
57 1
        $this->router = $router;
58 1
    }
59
60
    /**
61
     * @throws \LogicException
62
     *
63
     * @param mixed $data
64
     *
65
     * @return string
66
     */
67 2
    public function getLinkForFill($data)
68
    {
69 2
        if (!($this->router instanceof Router)) {
70 1
            throw new \LogicException('Link cannot be built without a Router');
71
        }
72
73 1
        return $this->router->generate(
74 1
            'fill_filler',
75
            [
76 1
                'plugin' => $this->getName(),
77 1
                $this->getForm()->getName() => ['url' => $data],
78
            ]
79 1
        );
80
    }
81
82
    /**
83
     * Fill from search result.
84
     *
85
     * @param ItemSearch $item
86
     *
87
     * @return Item|null
88
     */
89 4
    public function fillFromSearchResult(ItemSearch $item)
90
    {
91 4
        $query = parse_url($item->getLink(), PHP_URL_QUERY);
92 4
        parse_str($query, $query);
93 4
        if (empty($query[$this->getForm()->getName()])) {
94 3
            return;
95
        }
96
97 1
        return $this->fill($query[$this->getForm()->getName()]);
98
    }
99
100
    /**
101
     * @param string $url
102
     *
103
     * @return bool
104
     */
105
    public function isSupportedUrl($url)
106
    {
107
        return false;
108
    }
109
}
110