Completed
Push — try/capabilities ( 1eedd1...45f305 )
by
unknown
06:43
created

WPFilterRule::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * A rule which evaluates a filter and compares the output with a required value
4
 *
5
 * @package automattic/jetpack-capabilities
6
 */
7
8
namespace Automattic\Jetpack\Capabilities;
9
10
// phpcs:ignore Squiz.Commenting.ClassComment.Missing
11
class WPFilterRule implements Rule {
12
	/**
13
	 * The id of the filter to apply
14
	 *
15
	 * @var string filter_name
16
	 */
17
	private $filter_name;
18
19
	/**
20
	 * The value to be compared with the filter output
21
	 *
22
	 * @var string required_value
23
	 */
24
	private $required_value;
25
26
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
27
	public function __construct( $filter_name, $required_value, $initial_value = false ) {
28
		$this->filter_name    = $filter_name;
29
		$this->required_value = $required_value;
30
		$this->initial_value  = $initial_value;
0 ignored issues
show
Bug introduced by
The property initial_value 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...
31
	}
32
33
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
34
	public function check( ...$args ) {
35
		$result = apply_filters( $this->filter_name, $this->initial_value );
36
37
		if ( $result === $this->required_value ) {
38
			return new PermissionGranted();
39
		}
40
41
		return new PermissionDenied(
42
			// translators: Argument is a WordPress filter name.
43
			sprintf( __( 'Accessed blocked by filter %s', 'jetpack' ), $this->filter_name )
44
		);
45
	}
46
}
47