Completed
Push — develop ( e96e2d...4e806c )
by Agel_Nash
06:19
created

PhpCompat::entities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace EvolutionCMS\Legacy;
2
3
use EvolutionCMS\Interfaces\PhpCompatInterface;
4
5
/**
6
 * @TODO file_put_contents(), strftime(), mb_*()
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
7
 */
8
class PhpCompat implements PhpCompatInterface
9
{
10
    /**
11
     * @param string|array $str
12
     * @param int $flags
13
     * @param string $encode
14
     * @param int $safecount
15
     * @return string|array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

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...
16
     */
17
    public function htmlspecialchars($str = '', $flags = ENT_COMPAT, $encode = '', $safecount = 0)
18
    {
19
        $safecount++;
20
        $modx = evolutionCMS();
21
22
        if (1000 < $safecount) {
23
            exit("error too many loops '{$safecount}'");
0 ignored issues
show
Coding Style Compatibility introduced by
The method htmlspecialchars() 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...
24
        }
25
26
        if (is_array($str)) {
27
            foreach ($str as $i => $v) {
28
                $str[$i] = $this->htmlspecialchars($v, $flags, $encode, $safecount);
29
            }
30
        } elseif ($str !== '') {
31
            if ($encode !== '') {
32
                $encode = $modx->config['modx_charset'];
33
            }
34
            $ent_str = htmlspecialchars($str, $flags, $encode);
35
36
            if (!empty($str) && empty($ent_str)) {
37
                $detect_order = implode(',', mb_detect_order());
38
                $ent_str = mb_convert_encoding($str, $encode, $detect_order);
39
            }
40
        } else {
41
            $ent_str = '';
42
        }
43
44
        return $ent_str;
0 ignored issues
show
Bug introduced by
The variable $ent_str 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...
45
    }
46
47
    public function entities($data)
48
    {
49
        return entities($data, evolutionCMS()->getConfig('modx_charset', 'UTF-8'));
0 ignored issues
show
Bug introduced by
It seems like evolutionCMS()->getConfi...modx_charset', 'UTF-8') targeting EvolutionCMS\Core::getConfig() can also be of type boolean; however, entities() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
    }
51
}
52