Completed
Push — master ( 88dfbf...b0f2bc )
by
unknown
12:27
created

wps_barcode::add_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php if ( !defined( 'ABSPATH' ) ) exit;
2
class wps_barcode {
3
	public function __construct() {
4
		global $post_ID;
5
6
		$this->install();
7
		add_action( 'save_post', array($this, 'insert_barcode' ), 10, 3 );
8
	}
9
10
	/**
11
	 * Install default configuration in database if not present
12
	 */
13
	public function install() {
14
		global $table_prefix, $wpdb;
15
16
		/*Create config informations*/
17
		$conf = get_option('wps_barcode');
18
19
		if ( empty($conf) ) {
20
			$conf['generate_barcode'] = false;
21
			$conf['type'] = 'internal';
22
			$conf['internal_client'] = '040';
23
			$conf['internal_provider'] = '041';
24
			$conf['internal_invoice_client'] = '042';
25
			$conf['internal_do_client'] = '043';
26
			$conf['internal_product'] = '044';
27
			$conf['internal_assets_client'] = '050';
28
			$conf['internal_coupons'] = '051';
29
			$conf['internal_invoice_provider'] = '045';
30
			$conf['internal_do_provider'] = '046';
31
32
			$conf['normal_country_code'] = '320';
33
			$conf['normal_enterprise_code'] = '0001';
34
35
			add_option('wps_barcode', $conf);
36
37
			/*Adding barcode for existing products*/
38
			$posts = get_posts( array('posts_per_page' => -1 ,'post_type' => 'wpshop_product'));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $posts is correct as get_posts(array('posts_p...' => 'wpshop_product')) (which targets get_posts()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
39
			foreach ( $posts as $post ) {
0 ignored issues
show
Bug introduced by
The expression $posts of type null is not traversable.
Loading history...
40
				$meta = get_post_meta($post->ID);
41
42
				$attr = wpshop_attributes_set::getAttributeSetDetails($meta['_wpshop_product_attribute_set_id']);
43
				$output_order = array();
0 ignored issues
show
Unused Code introduced by
$output_order 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...
44
45
				if ( count($attr) > 0 ) {
46
					if (!empty($attr) ) {
47
						foreach ( $attr as $product_attr_group_id =>
0 ignored issues
show
Bug introduced by
The expression $attr of type string is not traversable.
Loading history...
48
								$product_attr_group_detail) {
49
							foreach ( $product_attr_group_detail['attribut']
50
									as $position => $attribute_def) {
51
								if ( !empty($attribute_def->code)
52
										AND $attribute_def->code === 'barcode') {
53
54
									$types_with_barcode = array(
55
											WPSHOP_IDENTIFIER_PRODUCT,
56
											WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
57
											WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION,
58
											WPSHOP_NEWTYPE_IDENTIFIER_COUPON,
59
											WPSHOP_NEWTYPE_IDENTIFIER_ORDER,
60
									);
61
62
									if ( !empty( $post->post_type ) &&
63
											in_array( $post->post_type,
64
											$types_with_barcode ) ) {
65
66
										$ref = '';
67
68 View Code Duplication
										if ( strlen($post->ID) < 5 ) {
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
											$length = 5-strlen($post->ID);
70
											for ($i = 0; $i < $length; $i++) {
71
												$ref .= '0';
72
											}
73
										}
74
75
										$ref .= strval($post->ID);
76
77 View Code Duplication
										if ( $conf['type'] === 'normal' ) {
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...
78
											$array['normal'] = array('country' =>
0 ignored issues
show
Coding Style Comprehensibility introduced by
$array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
79
													$conf['normal_country_code'],
80
													'enterprise' =>
81
													$conf['normal_enterprise_code'],'ID' => $ref);
82
										}
83
										else if ( $conf['type'] === 'internal' ) {
84
											$pDate = new DateTime($post->post_date);
85
											$date = $pDate->format('my');
86
87
											if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT ||
88
													$post->post_type === WPSHOP_IDENTIFIER_PRODUCT) {
89
												$type = $conf['internal_product'];
90
											}
91
											else if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_ORDER) {
92
												$type = $conf['internal_invoice_client'];
93
											}
94
											else if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_COUPON) {
95
												$type = $conf['internal_coupons'];
96
											}
97
											else {
98
												$type = '000';
99
											}
100
101
											$array['internal'] = array('type' => $type,
0 ignored issues
show
Coding Style Comprehensibility introduced by
$array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
102
													'date' => $date,
103
													'ID' => $ref);
104
										}
105
106
										$barcode = $this->wps_generate_barcode($array);
0 ignored issues
show
Bug introduced by
The variable $array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
107
										$data = array('entity_type_id' => 4,
108
												'attribute_id' => '12',
109
												'entity_id' => $post->ID,
110
												'user_id' => $post->post_author,
111
												'value' => $barcode,
112
												'creation_date_value' => date( 'Y-m-d H:i:s' ));
113
114
115
										$query = $wpdb->prepare( "
116
											SELECT *
117
											FROM " . WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR . "
118
											WHERE entity_id = %d
119
												AND attribute_id = (
120
													SELECT id
121
													FROM " . WPSHOP_DBT_ATTRIBUTE . "
122
													WHERE code = %s
123
												)" , $post->ID, 'barcode' );
124
											$result = $wpdb->get_results( $query, ARRAY_A );
125
126
										if ( empty($result) ) {
127
											$wpdb->insert($table_prefix.
128
													'wpshop__attribute_value_varchar', $data);
129
										} elseif( empty( $result[0]['value'] ) ) {
130
											$wpdb->update($table_prefix.
131
													'wpshop__attribute_value_varchar', $data, array( 'value_id' => $result[0]['value_id'] ) );
132
										}
133
									}
134
								}
135
							}
136
						}
137
					}
138
				}
139
			}
140
141
			/*Adding barcode for existing coupon*/
142
			$posts = get_posts( array('posts_per_page' => -1 ,'post_type' => 'wpshop_shop_coupon') );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $posts is correct as get_posts(array('posts_p... 'wpshop_shop_coupon')) (which targets get_posts()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
143
			foreach ( $posts as $post ) {
0 ignored issues
show
Bug introduced by
The expression $posts of type null is not traversable.
Loading history...
144
				$meta = get_post_meta($post->ID);
145
				if ( empty($meta['wpshop_coupon_barcode']) ) {
146
					if ( $conf['type'] === 'normal' ) {
147
						$array['normal'] = array('country' =>
148
								$conf['normal_country_code'],
149
								'enterprise' =>
150
								$conf['normal_enterprise_code'],'ID' => $ref);
0 ignored issues
show
Bug introduced by
The variable $ref does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
151
					}
152
					else if ( $conf['type'] === 'internal' ) {
153
						$pDate = new DateTime($post->post_date);
154
						$date = $pDate->format('my');
155
156
						if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_COUPON) {
157
							$type = $conf['internal_coupons'];
158
						}
159
						else {
160
							$type = '000';
161
						}
162
163
						$ref = '';
164 View Code Duplication
						if ( strlen($post->ID) < 5 ) {
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...
165
							$length = 5-strlen($post->ID);
166
							for ($i = 0; $i < $length; $i++) {
167
								$ref .= '0';
168
							}
169
						}
170
171
						$ref .= strval($post->ID);
172
						$array['internal'] = array('type' => $type,
173
								'date' => $date,
174
								'ID' => $ref);
175
					}
176
					$barcode = $this->wps_generate_barcode($array);
177
					update_post_meta($post->ID, 'wpshop_coupon_barcode', $barcode);
178
				}
179
			}
180
		}
181
	}
182
183
	/**
184
	 * Verifty post identifier and run a barcode generator
185
	 * @param string $post_ID Ident of post
186
	 */
187
	public function insert_barcode( $post_ID, $post, $update ) {
0 ignored issues
show
Unused Code introduced by
The parameter $update 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...
188
		if ( wp_is_post_revision( $post_ID ) )
189
			return;
190
191
		global $wpdb;
192
193
		$types_with_barcode = array(
194
			WPSHOP_IDENTIFIER_PRODUCT,
195
			WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
196
			WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION,
197
			WPSHOP_NEWTYPE_IDENTIFIER_COUPON,
198
			WPSHOP_NEWTYPE_IDENTIFIER_ORDER,
199
		);
200
201
		/** Si c'est un type qui est dans le tableau $types_with_barcode */
202
		if ( !empty( $post->post_type ) && in_array( $post->post_type,
203
				$types_with_barcode ) ) {
204
205
			$conf = get_option('wps_barcode');
206
			$ref = '';
207
208 View Code Duplication
			if ( strlen($post_ID) < 5 ) {
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...
209
				$length = 5-strlen($post_ID);
210
				for ($i = 0; $i < $length; $i++) {
211
					$ref .= '0';
212
				}
213
			}
214
215
			$ref .= strval($post_ID);
216
217 View Code Duplication
			if ( $conf['type'] === 'normal' ) {
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...
218
				$array['normal'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
219
					'country' 		=> $conf['normal_country_code'],
220
					'enterprise' 	=> $conf['normal_enterprise_code'],
221
					'ID' 			=> $ref,
222
				);
223
			}
224
			else if ( $conf['type'] === 'internal' ) {
225
				$pDate = new DateTime($post->post_date);
226
				$date = $pDate->format('my');
227
228
				if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT || $post->post_type === WPSHOP_IDENTIFIER_PRODUCT) {
229
					$type = $conf['internal_product'];
230
				}
231
				else if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_ORDER) {
232
					$type = $conf['internal_invoice_client'];
233
				}
234
				else if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_COUPON) {
235
					$type = $conf['internal_coupons'];
236
				}
237
				else {
238
					$type = '000';
239
				}
240
241
				$array['internal'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
242
					'type' => $type,
243
					'date' => $date,
244
					'ID' => $ref
245
				);
246
			}
247
248
			//$barcode = $this->wps_generate_barcode($array);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
249
			/** For add a product */
250
			$_REQUEST['wpshop_product_attribute']['varchar']['barcode'] = !empty( $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] ) ? sanitize_text_field( $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] ) : null;
251
			if( !isset($_REQUEST['wpshop_product_attribute']['varchar']['barcode']) ) {
252
				wpeologs_ctr::log_datas_in_files( 'wps_barcode',
253
				array(
254
					'object_id' => $post_ID,
255
					'message' => sprintf( __('Adding barcode: %s for %s object ID', 'wps_barcode'), '<b>'.$_REQUEST['wpshop_product_attribute']['varchar']['barcode'].'</b>', '<b>'.$post_ID.'</b>') ),
256
				0);
257
				$_REQUEST['wpshop_product_attribute']['varchar']['barcode'] = $this->wps_generate_barcode($array);
0 ignored issues
show
Bug introduced by
The variable $array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
258
			}
259
			/*if (  isset($barcode) ) {
260
				if ($barcode !== '') {
261
					wpeologs_ctr::log_datas_in_files( 'wps_barcode',
262
					array(
263
					'object_id' => $post_ID,
264
					'message' => sprintf( __('Change barcode: %s replacing %s for %s object ID', 'wps_barcode'), '<b>'.sanitize_text_field( $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] ).'</b>', '<b>'.$barcode.'</b>', '<b>'.$post_ID.'</b>') ), 0);
265
266
					$barcode = sanitize_text_field( $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] );
267
				}
268
				else {
269
					wpeologs_ctr::log_datas_in_files( 'wps_barcode',
270
					array(
271
						'object_id' => $post_ID,
272
						'message' => sprintf( __('Adding barcode: %s for %s object ID', 'wps_barcode'), '<b>'.$barcode.'</b>', '<b>'.$post_ID.'</b>') ),
273
					0);
274
275
					// $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] = $barcode;
276
				}
277
			}
278
			else {
279
				/** On met à jour l'attribut barcode *//*
280
				$products = new wps_product_ctr();
281
				$products->update_the_attribute_for_product($post_ID, 'varchar', 'barcode', $barcode);
282
			}*/
283
		}
284
	}
285
286
	/**
287
	 * Save post meta of the post
288
	 * @param string $post_type Constant identifier of post
289
	 * @param string $post_date Date of creation post
290
	 * @param string $post_ID Ident of post
291
	 * @return string Number code of barcode
292
	 */
293
	//private function wps_generate_barcode($post_type, $post_date, $post_ID) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
294
	private function wps_generate_barcode($array) {
295
		include_once('wps_barcodegen.ctr.php');
296
297
		$code = '';
298
		$ref = '';
0 ignored issues
show
Unused Code introduced by
$ref 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...
299
		$barcode = new wps_barcodegen;
300
301
		$conf = get_option('wps_barcode');
0 ignored issues
show
Unused Code introduced by
$conf 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...
302
303
		if ( array_key_exists('normal', $array) ) {
304
			$code .= $array['normal']['country'];
305
			$code .= $array['normal']['enterprise'];
306
			$code .= $array['normal']['ID'];
307
308
			$id = $array['normal']['ID'];
309
310
		}
311
		else if ( array_key_exists('internal', $array) ) {
312
			$code.= $array['internal']['type'];
313
			$code .= $array['internal']['date'];
314
			$code .= $array['internal']['ID'];
315
316
			$id = $array['internal']['ID'];
317
		}
318
319
		$gencode = $barcode->checksum($code);
320
321
		$barcode->writeLog( sprintf( __("Checksum generate: %s from %s <br />",
322
		'wps_barcode'), '<b>'.$gencode.'</b>',
323
		'<b>'.$code.'</b>') );
324
325
		$message = sprintf( __("Log generated by wps_barcodegen for add/update product: <br /> %s",
326
				'wps_barcode'), $barcode->getLog() );
327
328
		if ( class_exists('wpeologs_ctr') ) {
329
			wpeologs_ctr::log_datas_in_files( 'wps_barcode', array('object_id' => $id, 'message' => $message), 0);
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
330
		}
331
332
		return $gencode;
333
	}
334
}
335