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.

DeleteOperation   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 16.39 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 10
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_controls() 10 10 1
A validate() 0 4 1
B process() 0 29 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 the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
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 ICanBoogie\Module\Operation;
13
14
use ICanBoogie\ErrorCollection;
15
use ICanBoogie\Module;
16
use ICanBoogie\Operation;
17
18
/**
19
 * Deletes a record.
20
 */
21
class DeleteOperation extends Operation
22
{
23
	/**
24
	 * Modifies the following controls:
25
	 *
26
	 * - PERMISSION: MANAGE
27
	 * - RECORD: true
28
	 * - OWNERSHIP: true
29
	 */
30 View Code Duplication
	protected function get_controls()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
31
	{
32
		return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(self::CONTR...parent::get_controls(); (boolean[]) is incompatible with the return type of the parent method ICanBoogie\Operation::get_controls of type false[].

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...
33
34
			self::CONTROL_PERMISSION => Module::PERMISSION_MANAGE,
35
			self::CONTROL_RECORD => true,
36
			self::CONTROL_OWNERSHIP => true
37
38
		] + parent::get_controls();
39
	}
40
41
	/**
42
	 * @inheritdoc
43
	 */
44
	protected function validate(ErrorCollection $errors)
45
	{
46
		return true;
47
	}
48
49
	/**
50
	 * Delete the target record.
51
	 */
52
	protected function process()
53
	{
54
		$key = $this->key;
55
		$record = $this->record;
56
57
		if (!$record->delete())
58
		{
59
			throw new \RuntimeException($this->format('Unable to delete the record %key from %module.', [
60
61
				'key' => $key,
62
				'module' => $this->module->title
63
64
			]));
65
		}
66
67
		if ($this->request['redirect_to'])
68
		{
69
			$this->response->location = $this->request['redirect_to'];
70
		}
71
72
		$this->response->message = $this->format('The record %key has been deleted from %module.', [
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->format('The recor... $this->module->title)) of type object<ICanBoogie\I18n\FormattedString> is incompatible with the declared type string of property $message.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
74
			'key' => $key,
75
			'module' => $this->module->title
76
77
		]);
78
79
		return $key;
80
	}
81
}
82