Failed Conditions
Pull Request — master (#2)
by Arnold
06:44
created

file_functions.php ➔ file_contains()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4
Metric Value
cc 4
eloc 14
nc 4
nop 2
dl 0
loc 24
ccs 14
cts 14
cp 1
crap 4
rs 8.6845
1
<?php
2
3
namespace Jasny;
4
5
/**
6
 * Check if the file contains the specified string
7
 *
8
 * @string $filename
9
 * @string $str
10
 * @return boolean
11
 */
12
function file_contains($filename, $str)
0 ignored issues
show
Best Practice introduced by
The function Jasny\file_contains() has been defined more than once; this definition is ignored, only the first definition in global.php (L316-319) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
13
{
14 1
    $handle = fopen($filename, 'r');
15 1
    if (!$handle) return false;
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
16
    
17 1
    $valid = false;
18
    
19 1
    $len = max(2 * strlen($str), 256);
20 1
    $prev = '';
21
    
22 1
    while (!feof($handle)) {
23 1
        $cur = fread($handle, $len);
24
        
25 1
        if (strpos($prev . $cur, $str) !== false) {
26 1
            $valid = true;
27 1
            break;
28
        }
29 1
        $prev = $cur;
30 1
    }
31
    
32 1
    fclose($handle);
33
    
34 1
    return $valid;
35
}
36
37
/**
38
 * Match path against an extended wildcard pattern.
39
 *
40
 * @param string $pattern
41
 * @param string $path
42
 * @return boolean
43
 */
44
function fnmatch_extended($pattern, $path)
0 ignored issues
show
Best Practice introduced by
The function Jasny\fnmatch_extended() has been defined more than once; this definition is ignored, only the first definition in global.php (L328-331) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
45
{
46 24
    $quoted = preg_quote($pattern, '~');
47
    
48 24
    $step1 = strtr($quoted, ['\?' => '[^/]', '\*' => '[^/]*', '/\*\*' => '(?:/.*)?', '#' => '\d+', '\[' => '[',
49 24
        '\]' => ']', '\-' => '-', '\{' => '{', '\}' => '}']);
50
    
51
    $step2 = preg_replace_callback('~{[^}]+}~', function ($part) {
52 4
        return '(?:' . substr(strtr($part[0], ',', '|'), 1, -1) . ')';
53 24
    }, $step1);
54
    
55 24
    $regex = rawurldecode($step2);
56
57 24
    return (boolean)preg_match("~^{$regex}$~", $path);
58
}
59