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

Save_Meta_Class   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 1 1
B save_meta_data() 0 19 6
A save_single_meta_data() 0 14 2
A save_multiple_meta_data() 0 10 1
1
<?php
2
/**
3
 * Gestion des meta
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\Save_Meta_Class' ) ) {
18
	/**
19
	 * Gestion des meta
20
	 */
21
	class Save_Meta_Class extends Singleton_Util {
22
23
		/**
24
		 * Le constructeur
25
		 *
26
		 * @since 1.0.0.0
27
		 * @version 1.3.0.0
28
		 */
29
		protected function construct() {}
30
31
		/**
32
		 * Apelle la méthode selon si la définition du champ est en meta "single" ou "multiple".
33
		 *
34
		 * @param  object $object   L'objet courant.
35
		 * @param  string $function La méthode a appeler.
36
		 * @param  string $meta_key Le nom de la meta key.
37
		 *
38
		 * @since 1.0.0.0
39
		 * @ersion 1.3.0.0
40
		 */
41
		public static function save_meta_data( $object, $function, $meta_key ) {
42
			$schema = $object->get_model();
43
44
			$list_meta_json = array();
45
46
			if ( ! empty( $object->id ) ) {
47
				foreach ( $schema as $field_name => $field_def ) {
48
					if ( ! empty( $field_def['meta_type'] ) && isset( $object->$field_name ) ) {
49
						if ( 'single' === $field_def['meta_type'] ) {
50
							self::g()->save_single_meta_data( $object->id, $object->$field_name, $function, $field_def['field'] );
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 save_single_meta_data() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\Save_Meta_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...
51
						} else {
52
							$list_meta_json[ $field_name ] = $object->$field_name;
53
						}
54
					}
55
				}
56
57
				self::g()->save_multiple_meta_data( $object->id, $list_meta_json, $function, $meta_key );
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 save_multiple_meta_data() does only exist in the following sub-classes of eoxia\Singleton_Util: eoxia\Save_Meta_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...
58
			}
59
		}
60
61
		/**
62
		 * Sauvegarde la valeur dans une meta seul.
63
		 *
64
		 * @param int    $id       L'ID de l'élément.
65
		 * @param mixed  $value    La valeur a enregistrer.
66
		 * @param string $function La function a appeler.
67
		 * @param string $meta_key Le nom de la meta.
68
		 *
69
		 * @since 1.0.0.0
70
		 * @version 1.3.0.0
71
		 */
72
		private function save_single_meta_data( $id, $value, $function, $meta_key ) {
73
			$data = $value;
74
75
			if ( is_array( $data ) ) {
76
				$data = \wp_json_encode( $data );
77
				$data = addslashes( $data );
78
				$data = preg_replace_callback( '/\\\\u([0-9a-f]{4})/i', function ( $matches ) {
79
					$sym = mb_convert_encoding( pack( 'H*', $matches[1] ), 'UTF-8', 'UTF-16' );
80
					return $sym;
81
				}, $data );
82
			}
83
84
			call_user_func( $function, $id, $meta_key, $data );
85
		}
86
87
		/**
88
		 * Sauvegarde les valeurs dans une meta.
89
		 *
90
		 * @param int    $id          L'ID de l'élément.
91
		 * @param mixed  $array_value Les valeurs a enregistrer.
92
		 * @param string $function    La function a appeler.
93
		 * @param string $meta_key    Le nom de la meta.
94
		 *
95
		 * @since 1.0.0.0
96
		 * @version 1.3.0.0
97
		 */
98
		private function save_multiple_meta_data( $id, $array_value, $function, $meta_key ) {
99
			$data = \wp_json_encode( $array_value );
100
			$data = addslashes( $data );
101
			$data = preg_replace_callback( '/\\\\u([0-9a-f]{4})/i', function ( $matches ) {
102
				$sym = mb_convert_encoding( pack( 'H*', $matches[1] ), 'UTF-8', 'UTF-16' );
103
				return $sym;
104
			}, $data );
105
106
			call_user_func( $function, $id, $meta_key, $data );
107
		}
108
	}
109
} // 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...
110