Completed
Pull Request — master (#39)
by Phecho
05:53
created

FeedController::atomAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\ProjectNamespace;
17
use GrahamCampbell\Markdown\Facades\Markdown;
18
use Illuminate\Routing\Controller;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gitamin\Http\Controllers\Controller.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use Illuminate\Support\Str;
20
use Roumen\Feed\Facades\Feed;
21
22
class FeedController extends Controller
23
{
24
    /**
25
     * Feed facade.
26
     *
27
     * @var \Roumen\Feed\Facades\Feed
28
     */
29
    protected $feed;
30
31
    /**
32
     * Create a new feed controller instance.
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct()
37
    {
38
        $this->feed = Feed::make();
39
        $this->feed->title = Setting::get('app_name');
40
        $this->feed->description = trans('gitamin.feed');
41
        $this->feed->link = Str::canonicalize(Setting::get('app_domain'));
42
        $this->feed->setDateFormat('datetime');
43
    }
44
45
    /**
46
     * Generates an Atom feed of all issues.
47
     *
48
     * @param \Gitamin\Models\ProjectNamespace|null $namespace
49
     *
50
     * @return \Illuminate\Http\Response
51
     */
52
    public function atomAction(ProjectNamespace $namespace = null)
53
    {
54
        return $this->feedAction($namespace, false);
0 ignored issues
show
Bug introduced by
It seems like $namespace defined by parameter $namespace on line 52 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...
55
    }
56
57
    /**
58
     * Generates a Rss feed of all issues.
59
     *
60
     * @param \Gitamin\Models\ProjectNamespace|null $group
0 ignored issues
show
Bug introduced by
There is no parameter named $group. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
61
     *
62
     * @return \Illuminate\Http\Response
63
     */
64
    public function rssAction(ProjectNamespace $namespace = null)
65
    {
66
        $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...
67
68
        return $this->feedAction($namespace, true);
0 ignored issues
show
Bug introduced by
It seems like $namespace defined by parameter $namespace on line 64 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...
69
    }
70
71
    /**
72
     * Generates a feed of all issues.
73
     *
74
     * @param \Gitamin\Models\ProjectNamespace|null $namespace
75
     * @param bool                                  $isRss
76
     *
77
     * @return \Illuminate\Http\Response
78
     */
79
    private function feedAction(ProjectNamespace &$namespace, $isRss)
80
    {
81
        if ($namespace->exists) {
82
            $namespace->projects->map(function ($project) {
83
                $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...
84
                    $this->feedAddItem($issue, $isRss);
85
                });
86
            });
87
        } else {
88
            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...
89
                $this->feedAddItem($issue, $isRss);
90
            });
91
        }
92
93
        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...
94
    }
95
96
    /**
97
     * Adds an item to the feed.
98
     *
99
     * @param \Gitamin\Models\Issue $issue
100
     * @param bool                  $isRss
101
     */
102
    private function feedAddItem($issue, $isRss)
103
    {
104
        $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...
105
            $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...
106
            Setting::get('app_name'),
107
            Str::canonicalize(route('issue', ['id' => $issue->id])),
0 ignored issues
show
Documentation introduced by
The property id 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...
108
            $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...
109
            $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...
110
        );
111
    }
112
}
113