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

Config_Util   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 1 1
B init_config() 0 23 6
1
<?php
2
/**
3
 * Initialise les fichiers .config.json
4
 *
5
 * @package Evarisk\Plugin
6
 */
7
8
namespace eoxia;
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
if ( ! class_exists( '\eoxia\Config_Util' ) ) {
15
	/**
16
	 * Initialise les fichiers .config.json
17
	 *
18
	 * @author Jimmy Latour <[email protected]>
19
	 * @version 1.1.0.0
20
	 */
21
	class Config_Util extends \eoxia\Singleton_Util {
22
		/**
23
		 * Un tableau contenant toutes les configurations des fichies config.json
24
		 *
25
		 * @var array
26
		 */
27
		public static $init = array();
28
29
		/**
30
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
31
		 *
32
		 * @return void nothing
33
		 */
34
		protected function construct() {}
35
36
		/**
37
		 * Initialise les fichiers de configuration
38
		 *
39
		 * @param  string $path_to_config_file Le chemin vers le fichier config.json.
40
		 *
41
		 * @return mixed                       WP_Error si il ne trouve pas le fichier config du module
42
		 */
43
		public function init_config( $path_to_config_file, $plugin_slug = '' ) {
44
			if ( empty( $path_to_config_file ) ) {
45
				return new \WP_Error( 'broke', __( 'Impossible de charger le fichier', 'digirisk' ) );
46
			}
47
48
			$tmp_config = \eoxia\JSON_Util::g()->open_and_decode( $path_to_config_file );
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 open_and_decode() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\JSON_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...
49
50
			if ( empty( $tmp_config->slug ) ) {
51
				return new \WP_Error( 'broke', __( 'Le module nécessite un slug', 'digirisk' ) );
52
			}
53
54
			if ( ! empty( $plugin_slug ) ) {
55
				$slug = $tmp_config->slug;
56
				$tmp_config->path = self::$init[ $plugin_slug ]->path . $tmp_config->path;
57
				if ( isset( $tmp_config->external ) && ! empty( $tmp_config->external ) ) {
58
					self::$init['external']->$slug = $tmp_config;
59
				} else {
60
					self::$init[ $plugin_slug ]->$slug = $tmp_config;
61
				}
62
			} else {
63
				self::$init[ $tmp_config->slug ] = $tmp_config;
64
			}
65
		}
66
	}
67
} // 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...
68