Helper::endsWith()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
class Helper
6
{
7
	/**
8
	 * @param string $name
9
	 * @param string $path
10
	 * @return string
11
	 */
12
	public static function buildClassName( $name, $path = '' )
13
	{
14
		$className = array_map( 'ucfirst', array_map( 'strtolower', preg_split( '/[-_]/', $name )));
0 ignored issues
show
Bug introduced by
It seems like preg_split('/[-_]/', $name) can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

14
		$className = array_map( 'ucfirst', array_map( 'strtolower', /** @scrutinizer ignore-type */ preg_split( '/[-_]/', $name )));
Loading history...
15
		$className = implode( '', $className );
16
		return !empty( $path )
17
			? str_replace( '\\\\', '\\', sprintf( '%s\%s', $path, $className ))
18
			: $className;
19
	}
20
21
	/**
22
	 * @param string $name
23
	 * @param string $prefix
24
	 * @return string
25
	 */
26
	public static function buildMethodName( $name, $prefix = 'get' )
27
	{
28
		return lcfirst( static::buildClassName( $prefix . '-' . $name ));
29
	}
30
31
	/**
32
	 * @param string $needle
33
	 * @param string $haystack
34
	 * @return bool
35
	 */
36
	public static function endsWith( $needle, $haystack )
37
	{
38
		$length = strlen( $needle );
39
		return $length != 0
40
			? substr( $haystack, -$length ) === $needle
41
			: true;
42
	}
43
44
	/**
45
	 * @param mixed $fromClass
46
	 * @return string
47
	 */
48
	public static function getClassname( $fromClass )
49
	{
50
		$className = is_string( $fromClass )
51
			? $fromClass
52
			: get_class( $fromClass );
53
		$paths = explode( '\\', $className );
54
		return end( $paths );
55
	}
56
57
	/**
58
	 * get_current_screen() is unreliable because it is not defined on all admin pages.
59
	 * @return \WP_Screen|\stdClass
60
	 */
61
	public static function getCurrentScreen()
62
	{
63
		global $hook_suffix, $pagenow;
64
		if( function_exists( 'get_current_screen' )) {
65
			$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() 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...
66
		}
67
		if( empty( $screen )) {
68
			$screen = new \stdClass();
69
			$screen->base = $screen->id = $hook_suffix;
70
		}
71
		$screen->pagenow = $pagenow;
72
		return $screen;
73
	}
74
75
	/**
76
	 * @param string $needle
77
	 * @param string $haystack
78
	 * @return bool
79
	 */
80
	public static function startsWith( $needle, $haystack )
81
	{
82
		return substr( $haystack, 0, strlen( $needle )) === $needle;
83
	}
84
85
	/**
86
	 * @param mixed $value
87
	 * @return array
88
	 */
89
	public static function toArray( $value )
90
	{
91
		return array_filter( (array) $value );
92
	}
93
}
94