Completed
Push — master ( eef885...465d0c )
by
unknown
30:08 queued 12:49
created

External_Util   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 29.27 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 24
loc 82
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 1 1
A exec() 0 12 4
A inc_config() 0 3 1
B inc() 15 20 6
A inc_priority_file() 9 9 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
 * Gestion des externals
4
 *
5
 * @package Evarisk\Plugin
6
 */
7
8
namespace eoxia;
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
if ( ! class_exists( '\eoxia\External_Util' ) ) {
15
	/**
16
	 * Gestion des externals
17
	 *
18
	 * @author Jimmy Latour <[email protected]>
19
	 * @version 1.1.0.0
20
	 */
21
	class External_Util extends \eoxia\Singleton_Util {
22
		/**
23
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
24
		 *
25
		 * @return void nothing
26
		 */
27
		protected function construct() {}
28
29
		/**
30
		 * Parcours le fichier digirisk.config.json pour récupérer les chemins vers tous les modules.
31
		 * Initialise ensuite un par un, tous ses modules.
32
		 *
33
		 * @return mixed WP_Error si les configs de DigiRisk ne sont pas initialisés, ou si aucun module n'est présent.
34
		 */
35
		public function exec( $path, $plugin_slug ) {
36
			if ( empty( \eoxia\Config_Util::$init[ $plugin_slug ]->externals ) ) {
37
				return new \WP_Error( 'broke', __( 'Aucun module a charger', $plugin_slug ) );
38
			}
39
40
			if ( ! empty( \eoxia\Config_Util::$init[ $plugin_slug ]->externals ) ) {
41
				foreach ( \eoxia\Config_Util::$init[ $plugin_slug ]->externals as $external_json_path ) {
42
					self::inc_config( $plugin_slug, $path . $external_json_path );
43
					self::inc( $plugin_slug, $path . $external_json_path );
44
				}
45
			}
46
		}
47
48
		/**
49
		 * Appelle la méthode init_config de \eoxia\Config_Util pour initialiser les configs du module
50
		 *
51
		 * @param  string $module_json_path Le chemin vers le dossier du module.
52
		 * @return void                   	nothing
53
		 */
54
		public function inc_config( $plugin_slug, $module_json_path ) {
55
			\eoxia\Config_Util::g()->init_config( $module_json_path, $plugin_slug );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eoxia\Singleton_Util as the method init_config() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\Config_Util. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
56
		}
57
58
		/**
59
		 * Inclus les dépendences du module (qui sont défini dans le config.json du module en question)
60
		 *
61
		 * @param  string $module_json_path Le chemin vers le module.
62
		 * @return void                   	nothing
63
		 */
64
		public function inc( $plugin_slug, $module_json_path ) {
0 ignored issues
show
Unused Code introduced by
The parameter $plugin_slug 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...
65
			$module_name = basename( $module_json_path, '.config.json' );
0 ignored issues
show
Unused Code introduced by
$module_name 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...
66
			$module_path = dirname( $module_json_path );
0 ignored issues
show
Unused Code introduced by
$module_path 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...
67
68 View Code Duplication
			if ( ! empty( \eoxia\Config_Util::$init['external'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
				foreach ( \eoxia\Config_Util::$init['external'] as $external_name => $config ) {
70
					if ( ! empty( $config->dependencies ) ) {
71
						foreach ( $config->dependencies as $folder_name => $folder_conf ) {
72
							$path_to_module_and_dependence_folder = $config->path . '/' . $folder_name . '/';
73
74
							if ( ! empty( $folder_conf->priority ) ) {
75
								$this->inc_priority_file( $path_to_module_and_dependence_folder, $folder_name, $folder_conf->priority );
76
							}
77
78
							\eoxia\Include_util::g()->in_folder( $path_to_module_and_dependence_folder );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eoxia\Singleton_Util as the method in_folder() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\Include_Util. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
79
						}
80
					}
81
				}
82
			}
83
		}
84
85
		/**
86
		 * Inclus les fichiers prioritaires qui se trouvent dans la clé "priority" dans le .config.json du module
87
		 *
88
		 * @param  string $path_to_module_and_dependence_folder Le chemin vers le module.
89
		 * @param  string $dependence_folder                    le chemin vers le dossier à inclure.
90
		 * @param  array  $list_priority_file                    La liste des chemins des fichiers à inclure en priorité.
91
		 * @return void                                       	nothing
92
		 */
93 View Code Duplication
		public function inc_priority_file( $path_to_module_and_dependence_folder, $dependence_folder, $list_priority_file ) {
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...
94
			if ( ! empty( $list_priority_file ) ) {
95
				foreach ( $list_priority_file as $file_name ) {
96
					$path_file = realpath( $path_to_module_and_dependence_folder . $file_name . '.' . $dependence_folder . '.php' );
97
98
					require_once( $path_file );
99
				}
100
			}
101
		}
102
	}
103
} // End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104