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

Init_Util::construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Initialise le fichier digirisk.config.json et tous 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\Init_Util' ) ) {
15
	/**
16
	 * Cette classe initialise tous les fichiers config.json
17
	 *
18
	 * @author Jimmy Latour <[email protected]>
19
	 * @version 1.1.0.0
20
	 */
21
	class Init_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
		 * Appelles les méthodes read_core_util_file_and_include et init_main_config ainsi que init_module
31
		 *
32
		 * @return void nothing
33
		 */
34
		public function exec( $path, $plugin_slug ) {
35
			self::read_core_util_file_and_include( $path, $plugin_slug );
36
			self::init_main_config( $path, $plugin_slug );
37
			self::init_external( $path, $plugin_slug );
38
			Config_Util::$init['main'] = new \stdClass();
39
			Config_Util::$init['main']->full_plugin_path = $path;
40
			self::init_module( $path, $plugin_slug );
41
		}
42
43
		/**
44
		 * Listes la liste des fichiers ".utils" dans le dossier ./core/external/wpeo_util/
45
		 *
46
		 * @return mixed Si le dossier $path_to_core_folder_util n'existe pas ou si ce n'est pas un dossier, ça retourne un objet WP_Error
47
		 */
48
		private function read_core_util_file_and_include( $path, $plugin_slug ) {
49
			$path_to_core_folder_util = $path . 'core/external/wpeo_util/';
50
			if ( ! file_exists( $path_to_core_folder_util ) ) {
51
				return new \WP_Error( 'broke', __( 'Impossible de charger les fichiers .utils', $plugin_slug ) );
52
			}
53
54
			if ( ! is_dir( $path_to_core_folder_util ) ) {
55
				return new \WP_Error( 'broke', __( '$path_to_core_folder_util n\'est pas un dossier', $plugin_slug ) );
56
			}
57
58
			$list_file_name = scandir( $path_to_core_folder_util );
59
60
			if ( ! $list_file_name || ! is_array( $list_file_name ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $list_file_name of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
61
				return new \WP_Error( 'broke', __( 'Impossible de charger les fichiers .utils', $plugin_slug ) );
62
			}
63
64
			if ( ! empty( $list_file_name ) ) {
65
				foreach ( $list_file_name as $file_name ) {
66
					if ( '.' !== $file_name && '..' !== $file_name && 'index.php' !== $file_name && '.git' !== $file_name ) {
67
						$file_path = realpath( $path_to_core_folder_util . $file_name );
68
						require_once( $file_path );
69
					}
70
				}
71
			}
72
		}
73
74
		/**
75
		 * Appelle la méthode init_config avec le fichier digirisk.config.json
76
		 *
77
		 * @return void nothing
78
		 */
79
		private function init_main_config( $path, $plugin_slug ) {
80
			$main_config_path = $plugin_slug . '.config.json';
81
			\eoxia\Config_Util::g()->init_config( $path . $main_config_path );
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...
82
			Config_Util::$init[ $plugin_slug ]->path = $path;
83
		}
84
85
		private function init_external( $path, $plugin_slug ) {
86
			if ( empty( Config_Util::$init['external'] ) ) {
87
				Config_Util::$init['external'] = new \stdClass();
88
			}
89
90
			\eoxia\External_Util::g()->exec( $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 exec() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\External_Util, eoxia\Init_Util, eoxia\View_Util, eoxia\log_class. 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...
91
		}
92
93
		/**
94
		 * Appelle la méthode exec_module de \eoxia\Module_Util pour initialiser tous les modules
95
		 *
96
		 * @return void nothing
97
		 */
98
		private function init_module( $path, $plugin_slug ) {
99
			\eoxia\Module_Util::g()->exec_module( $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 exec_module() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\Module_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...
100
		}
101
	}
102
} // 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...
103