Completed
Push — master ( 97d2bc...038e4e )
by Tomáš
12:56
created

SecurityHelper::getResourcesNames()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
crap 4
1
<?php
2
3
/**
4
 * This file is part of Webcook security bundle.
5
 *
6
 * See LICENSE file in the root of the bundle. Webcook
7
 */
8
9
namespace Webcook\Cms\SecurityBundle\Common;
10
11
use Symfony\Component\Finder\Finder;
12
use Doctrine\Common\Annotations\AnnotationReader;
13
14
/**
15
 * Basic helper class.
16
 */
17
class SecurityHelper
18
{
19
    /**
20
     * Get system resources name.
21
     *
22
     *
23
     * @return array Array of system resources.
24 49
     */
25
    public static function getResourcesNames($path)
26 49
    {
27 49
        $resources = array();
28
        $finder = new Finder();
29 49
        try {
30 1
            $finder->files()->in($path)->name('*.php');
31 1
        } catch (\InvalidArgumentException $e) {
32
            return $resources;
33
        }
34 49
35 49
        $added = 0;
0 ignored issues
show
Unused Code introduced by
$added is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36 49
        foreach ($finder as $file) {
37 49
            $fileContent = file_get_contents($file->getRealPath());
38 49
            if (strpos($fileContent, '@ApiResource') !== false) {
39 49
                $resources[] = self::extractName($file->getRealPath(), str_replace('.php', '', $file->getFilename()));
40
            }
41
        }
42
43 49
        return $resources;
44
    }
45
46
    /**
47
     * Extract name from the controller path string.
48
     *
49
     * @param string $resourcePath Path of the controller.
50
     *
51
     * @return string Name.
52
     */
53 49
    public static function extractName($resourcePath, $name)
54
    {
55 49
        $resourcePath = str_replace('\\', '/', $resourcePath);
56
57 49
        return self::extract($resourcePath, 'Bundle').' - '.$name;
58
    }
59
60
    /**
61
     * Extract string.
62
     *
63
     * @param string $string String from which will be name extraced.
64
     * @param string $name   Extracted string.
65
     *
66
     * @return string Name.
67
     */
68 49
    private static function extract($string, $name)
69
    {
70 49
        preg_match("/^.*\/(.*)$name.*$/", $string, $matches);
71
72 49
        return count($matches) > 0 ? $matches[1] : null;
73
    }
74
}
75