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

Constructor_Data_Class   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
F dispatch_wordpress_data() 0 52 21
A set_default_data() 0 5 2
A do_wp_object() 0 11 4
1
<?php
2
/**
3
 * Gestion de la construction des données selon les modèles.
4
 *
5
 * @author Jimmy Latour <[email protected]>
6
 * @since 1.0.0.0
7
 * @version 1.3.0.0
8
 * @copyright 2015-2017
9
 * @package wpeo_model
10
 * @subpackage class
11
 */
12
13
namespace eoxia;
14
15
if ( ! defined( 'ABSPATH' ) ) { exit; }
16
17
if ( ! class_exists( '\eoxia\Constructor_Data_Class' ) ) {
18
	/**
19
	 * Gestion de la construction des données selon les modèles.
20
	 */
21
	class Constructor_Data_Class extends Helper_Class {
22
23
		/**
24
		 * Appelle la méthode pour dispatcher les données.
25
		 *
26
		 * @since 1.0.0.0
27
		 * @version 1.3.0.0
28
		 *
29
		 * @param Array $data Les données en brut.
30
		 */
31
		protected function __construct( $data ) {
32
			$this->dispatch_wordpress_data( $data, $data );
33
			Log_Class::g()->exec( 'digirisk_construct_data', '', __( 'Unable to transfer risk to wordpress system.', 'wp-digi-dtrans-i18n' ), array(
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...
34
				'object_id' => '',
35
				'object' => $this,
36
			), 0 );
37
		}
38
39
		/**
40
		 * Dispatches les données selon le modèle.
41
		 *
42
		 * @since 1.0.0.0
43
		 * @version 1.3.0.0
44
		 *
45
		 * @param array  $all_data       Toutes les données.
46
		 * @param array  $data           Les données actuelles.
47
		 * @param object $current_object L'objet en cours de construction.
48
		 * @param array  $model          La définition des données.
49
		 * @return object
50
		 */
51
		private function dispatch_wordpress_data( $all_data, $data, $current_object = null, $model = array() ) {
52
			if ( empty( $model ) ) {
53
				$model = $this->model;
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
			}
55
56
			if ( null === $current_object ) {
57
				$current_object = $this;
58
			}
59
60
			foreach ( $model as $field_name => $field_def ) {
61
				$current_object->$field_name = $this->set_default_data( $field_name, $field_def );
62
63
				// Est-ce qu'il existe des enfants ?
64
				if ( isset( $field_def['field'] ) && isset( $data[ $field_def['field'] ] ) && ! isset( $field_def['child'] ) ) {
65
					$current_object->$field_name = $data[ $field_def['field'] ];
66
				} elseif ( isset( $field_def['child'] ) ) {
67
					$current_data = ! empty( $all_data[ $field_name ] ) ? $all_data[ $field_name ] : array();
68
69
					if ( empty( $current_object->$field_name ) ) {
70
						$current_object->$field_name = new \stdClass();
71
					}
72
73
					$current_object->$field_name = $this->dispatch_wordpress_data( $all_data, $current_data, $current_object->$field_name, $field_def['child'] );
74
				}
75
76
				// Est-ce que le field_name existe en donnée (premier niveau) ?
77
				if ( isset( $data[ $field_name ] ) && isset( $field_def ) && ! isset( $field_def['child'] ) ) {
78
					$current_object->$field_name = $data[ $field_name ];
79
				}
80
81
				if ( isset( $field_def['required'] ) && $field_def['required'] && ! isset( $current_object->$field_name ) ) {
82
					$this->error = true;
0 ignored issues
show
Bug introduced by
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
83
				}
84
85
				if ( ! empty( $field_def['type'] ) ) {
86
					settype( $current_object->$field_name, $field_def['type'] );
87
					if ( 'string' === $field_def['type'] ) {
88
						$current_object->$field_name = stripslashes( $current_object->$field_name );
89
					}
90
91
					if ( ! empty( $field_def['array_type'] ) ) {
92
						if ( ! empty( $current_object->$field_name ) ) {
93
							foreach ( $current_object->$field_name as &$element ) {
94
								settype( $element, $field_def['array_type'] );
95
							}
96
						}
97
					}
98
				}
99
			} // End foreach().
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...
100
101
			return $current_object;
102
		}
103
104
		/**
105
		 * Si la définition bydefault existe, récupères la valeur.
106
		 *
107
		 * @since 1.0.0.0
108
		 * @version 1.3.0.0
109
		 *
110
		 * @param string $field_name Le nom du champ.
111
		 * @param array  $field_def  La définition du champ.
112
		 *
113
		 * @return mixed						 La donnée par défaut.
114
		 */
115
		private function set_default_data( $field_name, $field_def ) {
0 ignored issues
show
Unused Code introduced by
The parameter $field_name 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...
116
			if ( isset( $field_def['bydefault'] ) ) {
117
				return $field_def['bydefault'];
118
			}
119
		}
120
121
		/**
122
		 * Convertis le modèle en un tableau compatible WordPress.
123
		 *
124
		 * @since 1.0.0.0
125
		 * @version 1.3.0.0
126
		 *
127
		 * @return array Tableau compatible avec les fonctions WordPress.
128
		 */
129
		public function do_wp_object() {
130
			$object = array();
131
132
			foreach ( $this->model as $field_name => $field_def ) {
133
				if ( ! empty( $field_def['field'] ) && isset( $this->$field_name ) ) {
134
					$object[ $field_def['field'] ] = $this->$field_name;
135
				}
136
			}
137
138
			return $object;
139
		}
140
	}
141
} // 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...
142