Completed
Push — master ( a91da6...354ecf )
by Дмитрий
03:52
created

FunctionStringFormater::pass()   D

Complexity

Conditions 10
Paths 7

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 10
eloc 27
c 3
b 0
f 0
nc 7
nop 2
dl 0
loc 42
ccs 31
cts 31
cp 1
crap 10
rs 4.8196

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall;
4
5
use PHPSA\Context;
6
use PhpParser\Node\Expr\Array_;
7
use PhpParser\Node\Expr\FuncCall;
8
use PHPSA\Analyzer\Helper\DefaultMetadataPassTrait;
9
10
class FunctionStringFormater extends AbstractFunctionCallAnalyzer
11
{
12
    use DefaultMetadataPassTrait;
13
14
    const DESCRIPTION = 'Format string has same number of placeholders as parameters are passed into and forbid invalid type formats.';
15
16
    /**
17
     * @var array functions
18
     */
19
    protected static $functions = [
20
        'printf' => 'printf',
21
        'sprintf' => 'sprintf',
22
        'vprintf' => 'vprintf',
23
        'vsprintf' => 'vsprintf'
24
    ];
25
    /**
26
     * Placeholders for type format
27
     * @var array
28
     */
29
    protected $placeholders = [];
30
31
    /**
32
     * @param FuncCall $funcCall
33
     * @param Context $context
34
     * @return bool
35
     */
36 7
    public function pass(FuncCall $funcCall, Context $context)
37
    {
38 7
        $functionName = $this->resolveFunctionName($funcCall, $context);
39 7
        if ($functionName && isset(self::$functions[$functionName]) && $funcCall->args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $funcCall->args of type PhpParser\Node\Arg[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40 1
            $args = $funcCall->args;
41
42 1
            $formatCE = $context->getExpressionCompiler()->compile($args[0]);
43 1
            if ($formatCE->isString() && $formatCE->isCorrectValue()) {
44
                // get invalid placeholders
45 1
                preg_match_all("/(?<!\x25)\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?[^bcdeEufFgGosxX(%)]/", $formatCE->getValue(), $this->placeholders);
46 1
                if (count($this->placeholders[0]) > 0) {
47 1
                    $context->notice(
48 1
                        'function_format_type_invalid',
49 1
                        sprintf('Unexpected type format in %s function string', $functionName),
50
                        $funcCall
51 1
                    );
52 1
                } else {
53
                    // get valid placesholders
54 1
                    preg_match_all("/(?<!\x25)\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([bcdeEufFgGosxX])/", $formatCE->getValue(), $this->placeholders);
55 1
                    if ($args[1]->value instanceof Array_) {
56 1
                        if (count($this->placeholders[0]) !== count($args[1]->value->items)) {
0 ignored issues
show
Bug introduced by
The property items does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
57 1
                            $context->notice(
58 1
                                'function_array_length_invalid',
59 1
                                sprintf('Unexpected length of array passed to %s', $functionName),
60
                                $funcCall
61 1
                            );
62 1
                        }
63 1
                    } else {
64 1
                        if (count($this->placeholders[0]) !== (count($args) - 1)) {
65 1
                            $context->notice(
66 1
                                'function_arguments_length_invalid',
67 1
                                sprintf('Unexpected length of arguments passed to %s', $functionName),
68
                                $funcCall
69 1
                            );
70 1
                        }
71
                    }
72
                }
73 1
            }
74 1
        }
75
76 7
        return true;
77
    }
78
79
    /**
80
     * @return array
81
     */
82 2
    public function getRegister()
83
    {
84
        return [
85
            FuncCall::class
86 2
        ];
87
    }
88
}
89