Completed
Push — issue/1723 ( c371a2 )
by Ravinder
502:26 queued 485:02
created

error-tracking.php ➔ give_print_errors()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 8
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A error-tracking.php ➔ give_set_error() 0 8 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Error Tracking
4
 *
5
 * @package     Give
6
 * @subpackage  Functions/Errors
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Get Errors
19
 *
20
 * Retrieves all error messages stored during the checkout process.
21
 * If errors exist, they are returned.
22
 *
23
 * @since 1.0
24
 * @uses  Give_Session::get()
25
 * @return mixed array if errors are present, false if none found
26
 */
27
function give_get_errors() {
28
	return Give()->session->get( 'give_errors' );
29
}
30
31
/**
32
 * Set Error
33
 *
34
 * Stores an error in a session var.
35
 *
36
 * @since 1.0
37
 * @uses  Give_Session::get()
38
 *
39
 * @param int    $error_id      ID of the error being set.
40
 * @param string $error_message Message to store with the error.
41
 *
42
 * @return void
43
 */
44
function give_set_error( $error_id, $error_message ) {
45
	$errors = give_get_errors();
46
	if ( ! $errors ) {
47
		$errors = array();
48
	}
49
	$errors[ $error_id ] = $error_message;
50
	Give()->session->set( 'give_errors', $errors );
51
}
52
53
/**
54
 * Clears all stored errors.
55
 *
56
 * @since 1.0
57
 * @uses  Give_Session::set()
58
 * @return void
59
 */
60
function give_clear_errors() {
61
	Give()->session->set( 'give_errors', null );
62
}
63
64
/**
65
 * Removes (unsets) a stored error
66
 *
67
 * @since 1.0
68
 * @uses  Give_Session::set()
69
 *
70
 * @param int $error_id ID of the error being set.
71
 *
72
 * @return void
73
 */
74
function give_unset_error( $error_id ) {
75
	$errors = give_get_errors();
76
	if ( $errors ) {
77
		unset( $errors[ $error_id ] );
78
		Give()->session->set( 'give_errors', $errors );
79
	}
80
}
81
82
/**
83
 * Register die handler for give_die()
84
 *
85
 * @since  1.0
86
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
87
 */
88
function _give_die_handler() {
89
	if ( defined( 'GIVE_UNIT_TESTS' ) ) {
90
		return '_give_die_handler';
91
	} else {
92
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function _give_die_handler() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
93
	}
94
}
95
96
/**
97
 * Wrapper function for wp_die(). This function adds filters for wp_die() which
98
 * kills execution of the script using wp_die(). This allows us to then to work
99
 * with functions using give_die() in the unit tests.
100
 *
101
 * @since 1.0
102
 *
103
 * @param string $message Message to store with the error.
104
 * @param string $title   Error title.
105
 * @param int    $status  HTTP status code..
106
 *
107
 * @return void
108
 */
109
function give_die( $message = '', $title = '', $status = 400 ) {
110
	add_filter( 'wp_die_ajax_handler', '_give_die_handler', 10, 3 );
111
	add_filter( 'wp_die_handler', '_give_die_handler', 10, 3 );
112
	wp_die( $message, $title, array( 'response' => $status ) );
113
}
114