Completed
Push — master ( da7bd7...a4feba )
by Jean-Christophe
03:22
created

JString::getValueBetween()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 8
nc 2
nop 3
1
<?php
2
namespace Ajax\service;
3
class JString {
4
5
	public static function contains($hay,$needle){
6
		return strpos($hay, $needle) !== false;
7
	}
8
	public static function startswith($hay, $needle) {
9
		return substr($hay, 0, strlen($needle)) === $needle;
10
	}
11
12
	public static function endswith($hay, $needle) {
13
		return substr($hay, -strlen($needle)) === $needle;
14
	}
15
16
	public static function isNull($s){
17
		return (!isset($s) || NULL===$s || ""===$s);
18
	}
19
	public static function isNotNull($s){
20
		return (isset($s) && NULL!==$s && ""!==$s);
21
	}
22
23
	public static function isBoolean($value){
24
		return \is_bool($value) || $value==1 || $value==0;
25
	}
26
27
	public static function isBooleanTrue($value){
28
		return $value==1 || $value;
29
	}
30
31
	public static function isBooleanFalse($value){
32
		return $value==0 || !$value;
33
	}
34
35
	public static function camelCaseToSeparated($input,$separator=" "){
36
		return strtolower(preg_replace('/(?<!^)[A-Z]/', $separator.'$0', $input));
37
	}
38
39
	public static function replaceAtFirst($subject,$from, $to){
40
		$from = '/\A'.preg_quote($from, '/').'/';
41
		return \preg_replace($from, $to, $subject, 1);
42
	}
43
44
	public static function replaceAtLast($subject,$from, $to){
45
		$from = '/'.preg_quote($from, '/').'\z/';
46
		return \preg_replace($from, $to, $subject, 1);
47
	}
48
49
	public static function replaceAtFirstAndLast($subject,$fromFirst,$toFirst,$fromLast,$toLast){
50
		$s=self::replaceAtFirst($subject, $fromFirst, $toFirst);
51
		return self::replaceAtLast($s, $fromLast, $toLast);
52
	}
53
54
	public static function getValueBetween(&$str,$before="{{",$after="}}"){
55
		$matches=[];
56
		$_before=\preg_quote($before);
57
		$_after=\preg_quote($after);
58
		if(\preg_match('/'.$_before.'(.*?)'.$_after.'/s', $str, $matches)===1){
59
			$result=$matches[1];
60
			$str=\str_replace($before.$result.$after,"", $str);
61
		}
62
		return $result;
0 ignored issues
show
Bug introduced by
The variable $result 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...
63
	}
64
}