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 — 8.x-2.x ( acccba...703370 )
by Devin
03:51
created

RouteSubscriber::alterRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\df_tools_workflow\Routing;
4
5
use Drupal\Core\Routing\RouteSubscriberBase;
6
use Drupal\Core\Routing\RoutingEvents;
7
use Symfony\Component\Routing\RouteCollection;
8
9
/**
10
 * Class RouteSubscriber.
11
 *
12
 * @package Drupal\df_tools_workflow\Routing
13
 * Listens to the dynamic route events.
14
 */
15
class RouteSubscriber extends RouteSubscriberBase {
16
17
  /**
18
   * {@inheritdoc}
19
   */
20
  public static function getSubscribedEvents() {
21
    $events[RoutingEvents::ALTER] = ['onAlterRoutes',-9999];  // negative Values means "late"
0 ignored issues
show
Coding Style Comprehensibility introduced by
$events was never initialized. Although not strictly required by PHP, it is generally a good practice to add $events = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
22
    return $events;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $events; (array<*,array<string|integer>>) is incompatible with the return type of the parent method Drupal\Core\Routing\Rout...se::getSubscribedEvents of type array<*,string>.

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...
23
  }
24
25
  /**
26
   * {@inheritdoc}
27
   */
28
  protected function alterRoutes(RouteCollection $collection) {
29
    $collection->get('entity.node.latest_version')->setRequirements([
30
      '_entity_access' => 'node.view',
31
      '_permission' => 'view latest version,view any unpublished content',
32
      'node' => "\d+",
33
      '_method' => 'GET|POST|OPTIONS'
34
    ]);
35
  }
36
}
37