RandomApiMigration   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A pass() 0 15 3
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall;
7
8
use PhpParser\Node\Expr\FuncCall;
9
use PHPSA\Context;
10
11
class RandomApiMigration extends AbstractFunctionCallAnalyzer
12
{
13
    const DESCRIPTION = 'Checks for use of old rand, srand, getrandmax functions and suggests alternatives.';
14
15
    protected $map = [
16
        'rand' => 'mt_rand',
17
        'srand' => 'mt_srand',
18
        'getrandmax' => 'mt_getrandmax'
19
    ];
20
21
    public function pass(FuncCall $funcCall, Context $context)
22
    {
23
        $functionName = $this->resolveFunctionName($funcCall, $context);
24
        if ($functionName && isset($this->map[$functionName])) {
25
            $context->notice(
26
                'rand.api.migration',
27
                sprintf(
28
                    'Function %s() is not recommended, please use random_int/random_bytes (PHP 7) or mt_%s (not cryptographically secure) instead.',
29
                    $functionName,
30
                    $functionName
31
                ),
32
                $funcCall
33
            );
34
        }
35
    }
36
}
37