Completed
Push — master ( d64a29...6f20dd )
by Pol
02:49
created

ClassFinder::getDefinedNamespaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace drupol\Yaroc\Utilities;
4
5
/**
6
 * Class ClassFinder.
7
 *
8
 * @package drupol\Yaroc\Utilities
9
 */
10
class ClassFinder  {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after class name; 2 found
Loading history...
11
12
  /**
13
   *
14
   */
15
  const appRoot = __DIR__ . "/../../";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected APPROOT).
Loading history...
16
17
  /**
18
   * @param $namespace
19
   *
20
   * @return array
21
   */
22 13
  public static function getClassesInNamespace($namespace) {
23 13
    $files = scandir(self::getNamespaceDirectory($namespace));
24
25
    $classes = array_map(function($file) use ($namespace){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
26 13
      return $namespace . '\\' . str_replace('.php', '', $file);
27 13
    }, $files);
28
29 13
    return array_filter($classes, function($possibleClass){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
30 13
      return class_exists($possibleClass);
31 13
    });
32
  }
33
34
  /**
35
   * @return array
36
   */
37 13
  private static function getDefinedNamespaces() {
38 13
    $composerJsonPath = self::appRoot . 'composer.json';
39 13
    $composerConfig = json_decode(file_get_contents($composerJsonPath));
40
41
    //Apparently PHP doesn't like hyphens, so we use variable variables instead.
42 13
    $psr4 = "psr-4";
43 13
    return (array) $composerConfig->autoload->$psr4;
44
  }
45
46
  /**
47
   * @param $namespace
48
   *
49
   * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
50
   */
51 13
  private static function getNamespaceDirectory($namespace) {
52 13
    $composerNamespaces = self::getDefinedNamespaces();
53
54 13
    $namespaceFragments = explode('\\', $namespace);
55 13
    $undefinedNamespaceFragments = [];
56
57 13
    while($namespaceFragments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $namespaceFragments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Coding Style introduced by
Expected 1 space after WHILE keyword; 0 found
Loading history...
58 13
      $possibleNamespace = implode('\\', $namespaceFragments) . '\\';
59
60 13
      if(array_key_exists($possibleNamespace, $composerNamespaces)){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
61 13
        return realpath(self::appRoot . $composerNamespaces[$possibleNamespace] . implode('/', array_reverse($undefinedNamespaceFragments)));
62
      }
63
64 13
      $undefinedNamespaceFragments[] = array_pop($namespaceFragments);
65
    }
66
67
    return false;
68
  }
69
70
}
71