Completed
Push — master ( cfd04a...f58bf6 )
by Tobias
05:35
created

BasePHPVisitorTest::getSourceLocations()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8.0052

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 22
cts 23
cp 0.9565
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 24
nc 18
nop 2
crap 8.0052
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Extractor\Tests\Functional\Visitor\Php;
13
14
use Translation\Extractor\FileExtractor\PHPFileExtractor;
15
use Symfony\Component\Finder\Finder;
16
use Translation\Extractor\Model\SourceCollection;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
abstract class BasePHPVisitorTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @param $visitor
25
     * @param $namespaceForTestFile
26
     *
27
     * @return SourceCollection
28
     *
29
     * @throws \Exception
30
     */
31 13
    protected function getSourceLocations($visitor, $namespaceForTestFile)
32
    {
33 13
        $extractor = new PHPFileExtractor();
34 13
35
        if (is_array($visitor)) {
36 13
            foreach ($visitor as $nodeVisitor) {
37 13
                $extractor->addVisitor($nodeVisitor);
38 13
            }
39
        } else {
40 13
            $extractor->addVisitor($visitor);
41 13
        }
42 13
43
        $currentNamespace = explode('\\', __NAMESPACE__);
44 13
        $fileNamespace = explode('\\', $namespaceForTestFile);
45 13
        $filename = array_pop($fileNamespace).'*';
46 13
47 13
        $path = __DIR__.'/../../..';
48
        foreach ($fileNamespace as $i => $part) {
49 13
            if ($currentNamespace[$i] !== $part) {
50
                // Assert: The namespaces is different now
51 13
                for ($j = $i; $j < count($fileNamespace); ++$j) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
52 13
                    $path .= '/'.$fileNamespace[$j];
53
                }
54 13
55
                break;
56
            }
57
        }
58 13
59 13
        $finder = new Finder();
60 13
        $finder->files()->name($filename)->in($path);
61 13
62
        if ($finder->count() === 0) {
63 13
            throw new \Exception("Cannot find file for: $namespaceForTestFile. Tried path: $path - Maybe filename doesn't match the class?");
64
        }
65
66
        $collection = new SourceCollection();
67
        foreach ($finder as $file) {
68
            $extractor->getSourceLocations($file, $collection);
69
        }
70
71
        return $collection;
72
    }
73
}
74