Passed
Push — master ( 289cbe...3ce129 )
by Anton
03:29
created

Basic   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 15.87 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processItem() 0 4 2
A processContents() 0 4 1
A handleAjax() 0 21 4
A handle() 10 10 2

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
 * @package Cadmium\System\Modules\Extend
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules\Extend\Utils\Handler {
11
12
	use Modules\Extend, Ajax, Language, Request, Template;
13
14
	abstract class Basic extends Extend\Utils\Handler {
15
16
		/**
17
		 * Process the item block
18
		 */
19
20
		protected function processItem(Template\Block $item, array $data) {
21
22
			$item->class = (($data['name'] === $this->loader->get('name')) ? 'positive' : 'grey');
0 ignored issues
show
Documentation introduced by
The property class does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
23
		}
24
25
		/**
26
		 * Process the contents block
27
		 */
28
29
		protected function processContents(Template\Block $contents) {
30
31
			$contents->section = $this->loader->getSection();
0 ignored issues
show
Documentation introduced by
The property section does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
32
		}
33
34
		/**
35
		 * Handle the ajax request
36
		 */
37
38
		protected function handleAjax() : Ajax\Response {
39
40
			$ajax = Ajax::createResponse();
41
42
			# Process actions
43
44
			if (Request::post('action') === 'activate') {
45
46
				$name = Request::post('name');
47
48
				if (!$this->loader->activate($name, true)) return $ajax->setError(Language::get(static::$error_activate));
49
50
			} else if (Request::post('action') === 'list') {
51
52
				$ajax->set('items', $this->loader->getItems());
53
			}
54
55
			# ------------------------
56
57
			return $ajax;
58
		}
59
60
		/**
61
		 * Handle the request
62
		 *
63
		 * @return Template\Block|Ajax\Response : a block object if the ajax param was set to false, otherwise an ajax response
64
		 */
65
66 View Code Duplication
		public function handle(bool $ajax = false) {
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...
67
68
			$this->loader = new static::$loader_class(Request::get('list'));
69
70
			if ($ajax) return $this->handleAjax();
71
72
			# ------------------------
73
74
			return $this->getContents();
75
		}
76
	}
77
}
78