ExtensionFilter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A accept() 0 16 2
1
<?php
2
3
/*
4
 * This file is part of the kaloa/filesystem package.
5
 *
6
 * For full copyright and license information, please view the LICENSE file
7
 * that was distributed with this source code.
8
 */
9
10
namespace Kaloa\Filesystem;
11
12
use FilterIterator;
13
use Iterator;
14
use SplFileInfo;
15
16
/**
17
 * Example:
18
 *
19
 * <pre>
20
 * $path      = '/a/directory';
21
 * $whitelist = array('txt'); // List of file extensions to filter
22
 *
23
 * $iterator = new ExtensionFilterIterator(
24
 *                 new RecursiveIteratorIterator(
25
 *                     new RecursiveDirectoryIterator($path)),
26
 *                 $whitelist);
27
 *
28
 * foreach ($iterator as $file) {
29
 *     echo $file, "<br />";
30
 * }
31
 * </pre>
32
 *
33
 * @author Marc Ermshaus <[email protected]>
34
 */
35
final class ExtensionFilter extends FilterIterator
36
{
37
    /**
38
     * List of allowed file extensions
39
     *
40
     * @var array
41
     */
42
    private $whitelist;
43
44
    /**
45
     *
46
     * @param Iterator $iterator
47
     * @param array $whitelist
48
     */
49 1
    public function __construct(Iterator $iterator, array $whitelist)
50
    {
51 1
        parent::__construct($iterator);
52 1
        $this->whitelist = array_flip($whitelist);
53 1
    }
54
55
    /**
56
     *
57
     * @return boolean
58
     */
59 1
    public function accept()
60
    {
61
        /* @var $fileInfo SplFileInfo */
62 1
        $fileInfo = parent::current();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (current() instead of accept()). Are you sure this is correct? If so, you might want to change this to $this->current().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
63
64
        // Allow only files
65 1
        if (!$fileInfo->isFile()) {
66 1
            return false;
67
        }
68
69
        // Only allow file extensions from $whitelist
70
71 1
        $pi = pathinfo($fileInfo->getFilename());
72
73 1
        return (isset($this->whitelist[strtolower($pi['extension'])]));
74
    }
75
}
76