FindInFileUtil   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 57
c 2
b 0
f 0
dl 0
loc 114
rs 10
wmc 22

4 Methods

Rating   Name   Duplication   Size   Complexity  
B scandir2() 0 16 8
B findTxtt() 0 40 9
A strpos2() 0 7 2
A find() 0 18 3
1
<?php
2
3
namespace Azine\EmailBundle\Tests;
4
5
//Author		: de77
6
//Website		: www.de77.com
7
//License		: MIT (http://en.wikipedia.org/wiki/MIT_License)
8
//Class desc	: http://de77.com/php-class-fast-find-text-string-in-files-recursively
9
10
//------------------------------------------------------------------------------
11
//          If you like this class- please leave a comment on my site, thanks!
12
//------------------------------------------------------------------------------
13
14
class FindInFileUtil
15
{
16
    private $text;
17
    private $textlen;
18
    private $results;
19
20
    public $caseSensitive = true;
21
    public $error;
22
    public $excludeMode = true;
23
    public $formats = array('.jpg', '.gif', '.avi');
24
25
    /**
26
     * @param string $dir
27
     * @param string $text
28
     *
29
     * @return array of files with the search-text
30
     */
31
    public function find($dir, $text)
32
    {
33
        $this->textlen = strlen($text);
34
        $this->text = $text;
35
36
        if ($this->textlen > 4096) {
37
            $this->error = 'You cannot search for such long text. Limit is 4096 Bytes';
38
39
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
40
        }
41
42
        $this->results = array();
43
        if ('/' != substr($dir, -1, 1)) {
44
            $dir .= '/';
45
        }
46
        $this->scandir2($dir);
47
48
        return $this->results;
49
    }
50
51
    /**
52
     * @param string $file
53
     */
54
    private function findTxtt($file)
55
    {
56
        $ext = strrchr($file, '.');
57
58
        if (true == $this->excludeMode) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
59
            if (in_array($ext, $this->formats)) {
60
                return false;
61
            }
62
        } else {
63
            if (!in_array($ext, $this->formats)) {
64
                return false;
65
            }
66
        }
67
68
        if (filesize($file) < 40960) {
69
            $data = file_get_contents($file);
70
            if ($this->strpos2($data, $this->text)) {
71
                return true;
72
            }
73
74
            return false;
75
        }
76
        $f = fopen($file, 'r');
77
        $currentPos = 0;
78
        $step = 40960;
79
        while (!feof($f)) {
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        while (!feof(/** @scrutinizer ignore-type */ $f)) {
Loading history...
80
            $data = fread($f, $step);
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            $data = fread(/** @scrutinizer ignore-type */ $f, $step);
Loading history...
81
            if (!$data) {
82
                break;
83
            } elseif ($this->strpos2($data, $this->text)) {
84
                fclose($f);
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
                fclose(/** @scrutinizer ignore-type */ $f);
Loading history...
85
86
                return true;
87
            }
88
            $currentPos = $currentPos + $step - $this->textlen;
89
            fseek($f, $currentPos, SEEK_SET);
0 ignored issues
show
Bug introduced by
It seems like $f can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            fseek(/** @scrutinizer ignore-type */ $f, $currentPos, SEEK_SET);
Loading history...
90
        }
91
        fclose($f);
92
93
        return false;
94
    }
95
96
    /**
97
     * @param string $haystack
98
     * @param string $keyword
99
     */
100
    private function strpos2($haystack, $keyword)
101
    {
102
        if (!$this->caseSensitive) {
103
            return false !== mb_stripos($haystack, $keyword);
104
        }
105
106
        return false !== mb_strpos($haystack, $keyword);
107
    }
108
109
    /**
110
     * @param string $dir
111
     */
112
    private function scandir2($dir)
113
    {
114
        if (is_dir($dir)) {
115
            if ($dh = opendir($dir)) {
116
                while (false !== ($file = readdir($dh))) {
117
                    if ('.' != $file and '..' != $file) {
118
                        if (is_dir($dir.$file)) {
119
                            $this->scandir2($dir.$file.'/');
120
                        } else {
121
                            if ($this->findTxtt($dir.$file)) {
122
                                $this->results[] = $dir.$file;
123
                            }
124
                        }
125
                    }
126
                }
127
                closedir($dh);
128
            }
129
        }
130
    }
131
}
132