Completed
Push — master ( 2d0034...c3cc5e )
by Freek
02:04
created

Feed::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Feed;
4
5
use Illuminate\Http\Response as HttpResponse;
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
18
        if (!str_contains($feedConfiguration['items'], '@')) {
19
            throw InvalidConfiguration::delimiterNotPresent($feedConfiguration['items']);
20
        }
21
    }
22
23
    public function getFeedResponse() : HttpResponse
24
    {
25
        $feedContent = $this->getFeedContent($this->feedConfiguration);
0 ignored issues
show
Unused Code introduced by
The call to Feed::getFeedContent() has too many arguments starting with $this->feedConfiguration.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
26
27
        return response($feedContent, 200, ['Content-Type' => 'application/atom+xml; chartset=UTF-8']);
28
    }
29
30
    public function getFeedContent() : string
31
    {
32
        list($class, $method) = explode('@', $this->feedConfiguration['items']);
33
34
        $items = app($class)->$method();
35
36
        $meta = [
37
            'link' => $this->feedConfiguration['url'],
38
            'description' => $this->feedConfiguration['description'],
39
            'title' => $this->feedConfiguration['title'],
40
            'updated' => $this->getLastUpdatedDate($items, 'Y-m-d H:i:s'),
41
        ];
42
43
        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...
44
    }
45
46
    protected function getLastUpdatedDate(Collection $items, string $format) : string
47
    {
48
        if (!count($items)) {
49
            return '';
50
        }
51
52
        $lastItem = $items
53
            ->sortBy(function (FeedItem $feedItem) {
54
                return $feedItem->getFeedItemUpdated()->format('YmdHis');
55
            })
56
            ->last();
57
58
        return $lastItem->getFeedItemUpdated()->format($format);
59
    }
60
}
61