ExtenderPlugin   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 12
c 8
b 0
f 0
lcom 1
cbo 2
dl 89
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 9 9 1
A update() 11 11 1
A uninstall() 9 9 1
C processPlugin() 47 47 7
A validatePlugin() 5 5 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 namespace Comodojo\Installer\Actions;
2
3
use \Comodojo\Exception\InstallerException;
4
use \Exception;
5
6
/**
7
 * Comodojo Installer
8
 *
9
 * @package     Comodojo Framework
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     GPL-3.0+
12
 *
13
 * LICENSE:
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29 View Code Duplication
class ExtenderPlugin extends AbstractAction {
0 ignored issues
show
Duplication introduced by
This class 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...
30
31
    public function install($package_name, $package_extra) {
32
33
        $io = $this->getIO();
34
35
        $io->write("<info>>>> Installing (extender) plugins from package ".$package_name."</info>");
36
37
        $this->processPlugin($io, 'install', $package_name, $package_extra);
38
39
    }
40
41
    public function update($package_name, $initial_extra, $target_extra) {
42
43
        $io = $this->getIO();
44
45
        $io->write("<info>>>> Updating (extender) plugins from package ".$package_name."</info>");
46
47
        $this->processPlugin($io, 'uninstall', $package_name, $initial_extra);
48
49
        $this->processPlugin($io, 'install', $package_name, $target_extra);
50
51
    }
52
53
    public function uninstall($package_name, $package_extra) {
54
55
        $io = $this->getIO();
56
57
        $io->write("<info>>>> Removing (extender) plugins from package ".$package_name."</info>");
58
59
        $this->processPlugin($io, 'uninstall', $package_name, $package_extra);
60
61
    }
62
63
    private static function processPlugin($io, $action, $package_name, $package_extra) {
64
65
        foreach ($package_extra as $plugin => $configuration) {
66
67
            try {
68
69
                if ( !self::validatePlugin($configuration) ) throw new InstallerException('Skipping invalid plugin in '.$package_name);
70
71
                $plugin_name = $package_name.'-extender-'.$plugin;
72
                
73
                $event = $configuration["event"];
74
                
75
                $class = $configuration["class"];
76
                
77
                $method = empty($configuration["method"]) ? null : $configuration["method"];
78
                
79
                switch ($action) {
80
81
                    case 'install':
82
83
                        $this->getPackageInstaller()->plugins()->add($package_name, 'extender', $plugin_name, $event, $class, $method);
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
84
85
                        $io->write(" <info>+</info> enabled plugin ".$plugin_name." on event ".$event);
86
87
                        break;
88
89
                    case 'uninstall':
90
91
                        $id = $this->getPackageInstaller()->plugins()->byName($plugin_name)->getId();
92
93
                        $this->getPackageInstaller()->plugins()->delete($id);
94
95
                        $io->write(" <comment>-</comment> disabled plugin ".$plugin_name." on event ".$event);
96
97
                        break;
98
99
                }
100
101
            } catch (Exception $e) {
102
103
                $io->write('<error>Error processing plugin: '.$e->getMessage().'</error>');
104
105
            }
106
107
        }
108
109
    }
110
111
    private static function validatePlugin($plugin) {
112
113
        return !( empty($plugin["class"]) || empty($plugin["event"]) );
114
115
    }
116
117
}
118