Completed
Push — 0.4.26 ( 70c06b...d0497d )
by Peter
18:23
created

Filler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 8.24 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 91.67%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 8
c 5
b 0
f 1
lcom 1
cbo 5
dl 7
loc 85
ccs 22
cts 24
cp 0.9167
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getForm() 0 4 1
A setRouter() 0 4 1
A fillFromSearchResult() 0 9 2
A isSupportedUrl() 0 4 1
A buildMenu() 7 7 1
A getLinkForFill() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Filler;
12
13
use AnimeDb\Bundle\CatalogBundle\Plugin\Plugin;
14
use Knp\Menu\ItemInterface;
15
use AnimeDb\Bundle\CatalogBundle\Entity\Item;
16
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Filler as FillerForm;
17
use Symfony\Bundle\FrameworkBundle\Routing\Router;
18
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Item as ItemSearch;
19
20
/**
21
 * Plugin filler
22
 *
23
 * @package AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Filler
24
 * @author  Peter Gribanov <[email protected]>
25
 */
26
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...
27
{
28
    /**
29
     * @var Router
30
     */
31
    protected $router;
32
33
    /**
34
     * @param ItemInterface $item
35
     *
36
     * @return ItemInterface
37
     */
38 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...
39
    {
40 1
        return $item->addChild($this->getTitle(), [
41 1
            'route' => 'fill_filler',
42 1
            'routeParameters' => ['plugin' => $this->getName()]
43
        ]);
44
    }
45
46
    /**
47
     * @return Filler
48
     */
49 6
    public function getForm()
50
    {
51 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...
52
    }
53
54
    /**
55
     * @param Router $router
56
     */
57 1
    public function setRouter(Router $router)
58
    {
59 1
        $this->router = $router;
60 1
    }
61
62
    /**
63
     * @throws \LogicException
64
     *
65
     * @param mixed $data
66
     *
67
     * @return string
68
     */
69 2
    public function getLinkForFill($data)
70
    {
71 2
        if (!($this->router instanceof Router)) {
72 1
            throw new \LogicException('Link cannot be built without a Router');
73
        }
74
75 1
        return $this->router->generate(
76 1
            'fill_filler',
77
            [
78 1
                'plugin' => $this->getName(),
79 1
                $this->getForm()->getName() => ['url' => $data]
80
            ]
81
        );
82
    }
83
84
    /**
85
     * Fill from search result
86
     *
87
     * @param ItemSearch $item
88
     *
89
     * @return Item|null
90
     */
91 4
    public function fillFromSearchResult(ItemSearch $item)
92
    {
93 4
        $query = parse_url($item->getLink(), PHP_URL_QUERY);
94 4
        parse_str($query, $query);
95 4
        if (empty($query[$this->getForm()->getName()])) {
96 3
            return null;
97
        }
98 1
        return $this->fill($query[$this->getForm()->getName()]);
99
    }
100
101
    /**
102
     * @param string $url
103
     *
104
     * @return bool
105
     */
106
    public function isSupportedUrl($url)
107
    {
108
        return false;
109
    }
110
}
111