Collectable::call_func()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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 12 and the first side effect is on line 3.

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
namespace WFV\Abstraction;
3
defined( 'ABSPATH' ) || die();
4
5
use WFV\Contract\CollectionInterface;
6
7
/**
8
 *
9
 *
10
 * @since 0.10.0
11
 */
12
abstract class Collectable implements CollectionInterface {
13
14
	/**
15
	 *
16
	 *
17
	 * @since 0.10.0
18
	 * @access protected
19
	 * @var array
20
	 */
21
	protected $data = array();
22
23
	/**
24
	 * Check if the collection contains a key / value pair
25
	 *
26
	 * @since 0.10.0
27
	 *
28
	 * @param string $key
29
	 * @param string $value
30
	 * @return bool
31
	 */
32
	public function contains( $key = null, $value = null ) {
33
		if( $this->has( $key ) ) {
34
			return ( is_array( $this->data[ $key ] ) )
35
				? in_array( $value, $this->data[ $key ] )
36
				: $this->data[ $key ] === $value;
37
		}
38
		return false;
39
	}
40
41
	/**
42
	 * Returns escaped input value.
43
	 *
44
	 * @since 0.10.0
45
	 *
46
	 * @param string $key Index
47
	 * @param string|array (optional) $callback Context appropriate callable.
48
	 * @return string|null
49
	 */
50
	public function escape( $key, callable $callback = null ) {
51
		if( is_string( $key ) ) {
52
			$callback = ( null === $callback ) ? 'esc_html' : $callback;
53
			return ( true === $this->has( $key ) ) ? $this->call_func( $callback, $this->data[ $key ] ) : null;
0 ignored issues
show
Documentation introduced by
$callback is of type callable, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
		}
55
		return null;
56
	}
57
58
	/**
59
	 * Check if the collection has a given key
60
	 *
61
	 * @since 0.10.0
62
	 *
63
	 * @param string $key
64
	 * @return bool
65
	 */
66
	public function has( $key = null ) {
67
		return array_key_exists( $key, $this->data );
68
	}
69
70
	/**
71
	 * Checks if the collection has data
72
	 *
73
	 * @since 0.10.0
74
	 *
75
	 * @return bool
76
	 */
77
	public function is_populated() {
78
		return count( $this->data ) > 0;
79
	}
80
81
	/**
82
	 * If provided key is an array in this collection
83
	 *  pass each leaf through a callback and return new array.
84
	 *
85
	 * @since 0.10.0
86
	 *
87
	 * @param string $key
88
	 * @param callable $callback
89
	 * @return array
90
	 */
91
	public function transform( $key = null, callable $callback ) {
92
		// WIP
93
		if( true === $this->has( $key ) ) {
94
			if( is_array( $this->data[ $key ] ) ) {
95
				return $this->transform_array_leafs( $this->data[ $key ], $callback );
0 ignored issues
show
Documentation introduced by
$callback is of type callable, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
			}
97
		}
98
	}
99
100
	/**
101
	 * Transform $array leafs
102
	 *
103
	 * WIP
104
	 *
105
	 * @since 0.10.0
106
	 * @access private
107
	 *
108
	 * @param array $array
109
	 * @param string|array $callback
110
	 * @return array
111
	 */
112
	protected function transform_array_leafs( array $array, $callback ) {
113
		array_walk_recursive( $array, function( &$item ) use( $callback ) {
114
			$item = $this->call_func( $callback, $item );
115
		} );
116
		return $array;
117
	}
118
119
	/**
120
	 * Trigger a callback function
121
	 *
122
	 * @since 0.10.0
123
	 * @access private
124
	 *
125
	 * @param string|array $callback
126
	 * @param string (optional) $input The input string
127
	 * @return
128
	 */
129
	private function call_func( $callback, $input = null ) {
130
		// WIP - simplify
131
		if( is_array( $callback ) ) {
132
			$method = $callback[0];
133
			$args = $callback[1];
134
			if ( $input ) {
135
				array_unshift( $args, $input );
136
			}
137
			return call_user_func_array( $method, $args );
138
		}
139
		return $callback( $input );
140
	}
141
}
142