GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c188f7...035d67 )
by Romain
05:21
created

block_page_content()   C

Complexity

Conditions 19
Paths 2

Size

Total Lines 65
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 5.8746
c 0
b 0
f 0
cc 19
eloc 31
nc 2
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* namespaces.twig */
4
class __TwigTemplate_7b8f7f2f528d58b505743fb65d39227a6a06db1375e25a34ce46d4e331f6e74c extends Twig_Template
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
    public function __construct(Twig_Environment $env)
7
    {
8
        parent::__construct($env);
9
10
        // line 1
11
        $this->parent = $this->loadTemplate("layout/layout.twig", "namespaces.twig", 1);
12
        $this->blocks = array(
13
            'title' => array($this, 'block_title'),
14
            'body_class' => array($this, 'block_body_class'),
15
            'page_content' => array($this, 'block_page_content'),
16
        );
17
    }
18
19
    protected function doGetParent(array $context)
20
    {
21
        return "layout/layout.twig";
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'layout/layout.twig'; (string) is incompatible with the return type of the parent method Twig_Template::doGetParent of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
22
    }
23
24
    protected function doDisplay(array $context, array $blocks = array())
25
    {
26
        $this->parent->display($context, array_merge($this->blocks, $blocks));
0 ignored issues
show
Unused Code introduced by
The call to Twig_TemplateWrapper::display() has too many arguments starting with array_merge($this->blocks, $blocks).

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...
27
    }
28
29
    // line 2
30
    public function block_title($context, array $blocks = array())
31
    {
32
        echo "Namespaces | ";
33
        $this->displayParentBlock("title", $context, $blocks);
34
    }
35
36
    // line 3
37
    public function block_body_class($context, array $blocks = array())
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $blocks is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        echo "namespaces";
40
    }
41
42
    // line 5
43
    public function block_page_content($context, array $blocks = array())
0 ignored issues
show
Unused Code introduced by
The parameter $blocks is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        // line 6
46
        echo "    <div class=\"page-header\">
47
        <h1>Namespaces</h1>
48
    </div>
49
50
    ";
51
        // line 10
52
        if ((isset($context["namespaces"]) || array_key_exists("namespaces", $context) ? $context["namespaces"] : (function () { throw new Twig_Error_Runtime('Variable "namespaces" does not exist.', 10, $this->getSourceContext()); })())) {
53
            // line 11
54
            echo "        <div class=\"namespaces clearfix\">
55
            ";
56
            // line 12
57
            $context["last_name"] = "";
58
            // line 13
59
            echo "            ";
60
            $context['_parent'] = $context;
61
            $context['_seq'] = twig_ensure_traversable((isset($context["namespaces"]) || array_key_exists("namespaces", $context) ? $context["namespaces"] : (function () { throw new Twig_Error_Runtime('Variable "namespaces" does not exist.', 13, $this->getSourceContext()); })()));
62
            foreach ($context['_seq'] as $context["_key"] => $context["namespace"]) {
63
                // line 14
64
                echo "                ";
65
                $context["top_level"] = twig_first($this->env, twig_split_filter($this->env, $context["namespace"], "\\"));
66
                // line 15
67
                echo "                ";
68
                if (((isset($context["top_level"]) || array_key_exists("top_level", $context) ? $context["top_level"] : (function () { throw new Twig_Error_Runtime('Variable "top_level" does not exist.', 15, $this->getSourceContext()); })()) != (isset($context["last_name"]) || array_key_exists("last_name", $context) ? $context["last_name"] : (function () { throw new Twig_Error_Runtime('Variable "last_name" does not exist.', 15, $this->getSourceContext()); })()))) {
69
                    // line 16
70
                    echo "                    ";
71
                    if ((isset($context["last_name"]) || array_key_exists("last_name", $context) ? $context["last_name"] : (function () { throw new Twig_Error_Runtime('Variable "last_name" does not exist.', 16, $this->getSourceContext()); })())) {
72
                        echo "</ul></div>";
73
                    }
74
                    // line 17
75
                    echo "                    <div class=\"namespace-container\">
76
                        <h2>";
77
                    // line 18
78
                    echo (isset($context["top_level"]) || array_key_exists("top_level", $context) ? $context["top_level"] : (function () { throw new Twig_Error_Runtime('Variable "top_level" does not exist.', 18, $this->getSourceContext()); })());
79
                    echo "</h2>
80
                        <ul>
81
                    ";
82
                    // line 20
83
                    $context["last_name"] = (isset($context["top_level"]) || array_key_exists("top_level", $context) ? $context["top_level"] : (function () { throw new Twig_Error_Runtime('Variable "top_level" does not exist.', 20, $this->getSourceContext()); })());
84
                    // line 21
85
                    echo "                ";
86
                }
87
                // line 22
88
                echo "                <li><a href=\"";
89
                echo $this->env->getExtension('Sami\Renderer\TwigExtension')->pathForNamespace($context, $context["namespace"]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twig_ExtensionInterface as the method pathForNamespace() does only exist in the following implementations of said interface: Sami\Renderer\TwigExtension.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
90
                echo "\">";
91
                echo $context["namespace"];
92
                echo "</a></li>
93
            ";
94
            }
95
            $_parent = $context['_parent'];
96
            unset($context['_seq'], $context['_iterated'], $context['_key'], $context['namespace'], $context['_parent'], $context['loop']);
97
            $context = array_intersect_key($context, $_parent) + $_parent;
0 ignored issues
show
Unused Code introduced by
$context is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
            // line 24
99
            echo "                </ul>
100
            </div>
101
        </div>
102
    ";
103
        }
104
        // line 28
105
        echo "
106
";
107
    }
108
109
    public function getTemplateName()
110
    {
111
        return "namespaces.twig";
112
    }
113
114
    public function isTraitable()
115
    {
116
        return false;
117
    }
118
119
    public function getDebugInfo()
120
    {
121
        return array (  105 => 28,  99 => 24,  88 => 22,  85 => 21,  83 => 20,  78 => 18,  75 => 17,  70 => 16,  67 => 15,  64 => 14,  59 => 13,  57 => 12,  54 => 11,  52 => 10,  46 => 6,  43 => 5,  37 => 3,  30 => 2,  11 => 1,);
122
    }
123
124
    public function getSourceContext()
125
    {
126
        return new Twig_Source("{% extends \"layout/layout.twig\" %}
127
{% block title %}Namespaces | {{ parent() }}{% endblock %}
128
{% block body_class 'namespaces' %}
129
130
{% block page_content %}
131
    <div class=\"page-header\">
132
        <h1>Namespaces</h1>
133
    </div>
134
135
    {% if namespaces %}
136
        <div class=\"namespaces clearfix\">
137
            {% set last_name = '' %}
138
            {% for namespace in namespaces %}
139
                {% set top_level = namespace|split('\\\\')|first %}
140
                {% if top_level != last_name %}
141
                    {% if last_name %}</ul></div>{% endif %}
142
                    <div class=\"namespace-container\">
143
                        <h2>{{ top_level|raw }}</h2>
144
                        <ul>
145
                    {% set last_name = top_level %}
146
                {% endif %}
147
                <li><a href=\"{{ namespace_path(namespace) }}\">{{ namespace|raw }}</a></li>
148
            {% endfor %}
149
                </ul>
150
            </div>
151
        </div>
152
    {% endif %}
153
154
{% endblock %}
155
", "namespaces.twig", "phar:///Users/bobby/Dropbox/Sites/OpenSource/Slackify/sami.phar/Sami/Resources/themes/default/namespaces.twig");
156
    }
157
}
158