FeedController::feedAction()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 16
rs 9.4286
cc 3
eloc 9
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
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 Gitamin\Http\Controllers;
13
14
use Gitamin\Facades\Setting;
15
use Gitamin\Models\Issue;
16
use Gitamin\Models\Owner;
17
use GrahamCampbell\Markdown\Facades\Markdown;
18
use Illuminate\Support\Str;
19
use Roumen\Feed\Facades\Feed;
20
21
class FeedController extends Controller
22
{
23
    /**
24
     * Feed facade.
25
     *
26
     * @var \Roumen\Feed\Facades\Feed
27
     */
28
    protected $feed;
29
30
    /**
31
     * Create a new feed controller instance.
32
     */
33
    public function __construct()
34
    {
35
        $this->feed = Feed::make();
36
        $this->feed->title = Setting::get('app_name');
37
        $this->feed->description = trans('gitamin.feed');
38
        $this->feed->link = Str::canonicalize(Setting::get('app_domain'));
39
        $this->feed->setDateFormat('datetime');
40
    }
41
42
    /**
43
     * Generates an Atom feed of all issues.
44
     *
45
     * @param \Gitamin\Models\Owner|null $owner
46
     *
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function atomAction(Owner $owner = null)
50
    {
51
        return $this->feedAction($owner, false);
0 ignored issues
show
Bug introduced by
It seems like $owner defined by parameter $owner on line 49 can be null; however, Gitamin\Http\Controllers...ontroller::feedAction() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
52
    }
53
54
    /**
55
     * Generates a Rss feed of all issues.
56
     *
57
     * @param \Gitamin\Models\Owner|null $owner
58
     *
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function rssAction(Owner $owner = null)
62
    {
63
        $this->feed->lang = Setting::get('app_locale');
0 ignored issues
show
Bug introduced by
The property lang does not seem to exist in Roumen\Feed\Facades\Feed.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
64
65
        return $this->feedAction($owner, true);
0 ignored issues
show
Bug introduced by
It seems like $owner defined by parameter $owner on line 61 can be null; however, Gitamin\Http\Controllers...ontroller::feedAction() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
66
    }
67
68
    /**
69
     * Generates a feed of all issues.
70
     *
71
     * @param \Gitamin\Models\Owner|null $owner
72
     * @param bool                       $isRss
73
     *
74
     * @return \Illuminate\Http\Response
75
     */
76
    private function feedAction(Owner &$owner, $isRss)
77
    {
78
        if ($owner->exists) {
79
            $owner->projects->map(function ($project) {
0 ignored issues
show
Documentation introduced by
The property projects does not exist on object<Gitamin\Models\Owner>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
80
                $project->issues()->visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) use ($isRss) {
0 ignored issues
show
Bug introduced by
The variable $isRss does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
81
                    $this->feedAddItem($issue, $isRss);
82
                });
83
            });
84
        } else {
85
            Issue::visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) use ($isRss) {
0 ignored issues
show
Bug introduced by
The method visible() does not exist on Gitamin\Models\Issue. Did you maybe mean scopeVisible()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
86
                $this->feedAddItem($issue, $isRss);
87
            });
88
        }
89
90
        return $this->feed->render($isRss ? 'rss' : 'atom');
0 ignored issues
show
Bug introduced by
The method render() does not seem to exist on object<Roumen\Feed\Facades\Feed>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
    }
92
93
    /**
94
     * Adds an item to the feed.
95
     *
96
     * @param \Gitamin\Models\Issue $issue
97
     * @param bool                  $isRss
98
     */
99
    private function feedAddItem($issue, $isRss)
100
    {
101
        $this->feed->add(
0 ignored issues
show
Bug introduced by
The method add() does not seem to exist on object<Roumen\Feed\Facades\Feed>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
            $issue->name,
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
            Setting::get('app_name'),
104
            Str::canonicalize($issue->url),
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
            $isRss ? $issue->created_at->toRssString() : $issue->created_at->toAtomString(),
0 ignored issues
show
Documentation introduced by
The property created_at does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
            $isRss ? $issue->message : Markdown::convertToHtml($issue->message)
0 ignored issues
show
Documentation introduced by
The property message does not exist on object<Gitamin\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
        );
108
    }
109
}
110