Completed
Pull Request — master (#8)
by Phecho
03:46
created

FeedController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 27.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 22
loc 80
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A rssAction() 11 19 2
A atomAction() 11 16 2
A feedAddItem() 0 10 3

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
/*
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\ProjectTeam;
17
use Illuminate\Routing\Controller;
18
use Illuminate\Support\Str;
19
use Roumen\Feed\Facades\Feed;
20
21
class FeedController extends Controller
22
{
23
    protected $feed;
24
25
    public function __construct()
26
    {
27
        $this->feed = Feed::make();
28
        $this->feed->title = Setting::get('app_name');
29
        $this->feed->description = trans('gitamin.feed');
30
        $this->feed->link = Str::canonicalize(Setting::get('app_domain'));
31
        $this->feed->setDateFormat('datetime');
32
    }
33
    /**
34
     * Generates a Rss feed of all issues.
35
     *
36
     * @param \Gitamin\Models\ProjectTeam|null $group
37
     *
38
     * @return \Illuminate\Http\Response
39
     */
40
    public function rssAction(ProjectTeam $group = null)
41
    {
42
        
43
        $this->feed->lang = Setting::get('app_locale');
44
        
45 View Code Duplication
        if ($group->exists) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
46
            $group->projects->map(function ($project) {
0 ignored issues
show
Documentation introduced by
The property projects does not exist on object<Gitamin\Models\ProjectTeam>. 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...
47
                $project->issues()->visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) {
48
                    $this->feedAddItem($issue);
49
                });
50
            });
51
        } else {
52
            Issue::visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) {
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...
53
                $this->feedAddItem($issue);
54
            });
55
        }
56
57
        return $this->feed->render('rss');
58
    }
59
    
60
    /**
61
     * Generates an Atom feed of all issues.
62
     *
63
     * @param \Gitamin\Models\ProjectTeam|null $group
64
     *
65
     * @return \Illuminate\Http\Response
66
     */
67
    public function atomAction(ProjectTeam $group = null)
68
    {
69 View Code Duplication
        if ($group->exists) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70
            $group->projects->map(function ($project) {
0 ignored issues
show
Documentation introduced by
The property projects does not exist on object<Gitamin\Models\ProjectTeam>. 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...
71
                $project->issues()->visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) {
72
                    $this->feedAddItem($issue, false);
73
                });
74
            });
75
        } else {
76
            Issue::visible()->orderBy('created_at', 'desc')->get()->map(function ($issue) {
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...
77
                $this->feedAddItem($issue, false);
78
            });
79
        }
80
81
        return $this->feed->render('atom');
82
    }
83
84
    /**
85
     * Adds an item to the feed.
86
     *
87
     * @param \Gitamin\Models\Issue     $issue
88
     * @param bool                      $isRss
89
     */
90
    private function feedAddItem($issue, $isRss = true)
91
    {
92
        $this->feed->add(
93
            $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...
94
            Setting::get('app_name'),
95
            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...
96
            $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...
97
            $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...
98
        );
99
    }
100
}
101