Completed
Pull Request — master (#38)
by
unknown
02:40
created

Feed   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 16
loc 60
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getFeedResponse() 0 4 1
A getFeedContent() 0 10 1
A getFeedConfigurationItems() 8 8 2
A getFeedConfigurationItemsFilter() 8 8 2
A getLastUpdatedDate() 0 12 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
namespace Spatie\Feed;
4
5
use Illuminate\Http\Response;
6
use Illuminate\Support\Collection;
7
use Spatie\Feed\Exceptions\InvalidConfiguration;
8
9
class Feed
10
{
11
    /** @var array */
12
    protected $feedConfiguration;
13
14
    public function __construct(array $feedConfiguration)
15
    {
16
        $this->feedConfiguration = $feedConfiguration;
17
        if (! str_contains($this->getFeedConfigurationItems(), '@')) {
18
            throw InvalidConfiguration::delimiterNotPresent($this->getFeedConfigurationItems());
19
        }
20
    }
21
22
    public function getFeedResponse()
23
    {
24
        return response($this->getFeedContent(), 200, ['Content-Type' => 'application/xml;charset=UTF-8']);
25
    }
26
27
    public function getFeedContent()
28
    {
29
        list($class, $method) = explode('@', $this->getFeedConfigurationItems());
30
31
        $items = app($class)->{$method}($this->getFeedConfigurationItemsFilter());
32
33
        $meta = ['id' => url($this->feedConfiguration['url']), 'link' => url($this->feedConfiguration['url']), 'title' => $this->feedConfiguration['title'], 'updated' => $this->getLastUpdatedDate($items)];
34
35
        return view('laravel-feed::feed', compact('meta', 'items'))->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
36
    }
37
38 View Code Duplication
    protected function getFeedConfigurationItems()
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
        if (is_array($this->feedConfiguration['items'])) {
41
            return $this->feedConfiguration['items'][0];
42
        }
43
44
        return $this->feedConfiguration['items'];
45
    }
46
47 View Code Duplication
    protected function getFeedConfigurationItemsFilter()
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...
48
    {
49
        if (is_array($this->feedConfiguration['items'])) {
50
            return $this->feedConfiguration['items'][1];
51
        }
52
53
        return null;
54
    }
55
56
    protected function getLastUpdatedDate(Collection $items)
57
    {
58
        if (! count($items)) {
59
            return '';
60
        }
61
62
        $lastItem = $items->sortBy(function (FeedItem $feedItem) {
63
            return $feedItem->getFeedItemUpdated()->format('YmdHis');
64
        })->last();
65
66
        return $lastItem->getFeedItemUpdated()->toAtomString();
67
    }
68
}
69