Completed
Push — trunk ( c7e339...34029c )
by SuperNova.WS
04:10
created

Autoloader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 40.54%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 15
cts 37
cp 0.4054
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
B autoloader() 0 15 7
A reset() 0 2 1
A register() 0 18 4
A _constructorStatic() 0 4 2
1
<?php
2
/**
3
 * Created by Gorlum 18.06.2017 13:24
4
 */
5
6
namespace Core;
7
8
/**
9
 * Class Autoloader
10
 *
11
 * One of core class to supply autoload facilities to the engine
12
 *
13
 * @package Core
14
 */
15
class Autoloader {
16
  const P_FOLDER = 'P_FOLDER';
17
  const P_PREFIX = 'P_PREFIX';
18
19
  /**
20
   * @var string[][] $folders - [[P_FOLDER => (str)$absoluteFolder, P_PREFIX => (str)$prefixToIgnore]]
21
   */
22
  protected static $folders = [];
23
24
  protected static $autoloaderRegistered = false;
25
26 1
  protected static function _constructorStatic() {
27 1
    if(!static::$autoloaderRegistered) {
28
      spl_autoload_register(array(__CLASS__, 'autoloader'));
29
      static::$autoloaderRegistered = true;
30
    }
31 1
  }
32
33
  /**
34
   * @param string $class - Fully-qualified path with namespaces
35
   */
36 1
  public static function autoloader($class) {
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
37 1
    static::_constructorStatic();
38
39 1
    foreach(static::$folders as $data) {
40 1
      $theClassFile = $class;
41
42 1
      if($data[static::P_PREFIX] && strrpos($class, $data[static::P_PREFIX]) !== false) {
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type object; however, parameter $haystack of strrpos() does only seem to accept string, 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

42
      if($data[static::P_PREFIX] && strrpos(/** @scrutinizer ignore-type */ $class, $data[static::P_PREFIX]) !== false) {
Loading history...
43
        $theClassFile = substr($class, strlen($data[static::P_PREFIX]));
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type object; however, parameter $string of substr() does only seem to accept string, 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

43
        $theClassFile = substr(/** @scrutinizer ignore-type */ $class, strlen($data[static::P_PREFIX]));
Loading history...
44
      }
45
46 1
      $classFullFileName = str_replace('\\', '/', $data[static::P_FOLDER] . $theClassFile) . DOT_PHP_EX;
0 ignored issues
show
Bug introduced by
Are you sure $theClassFile of type object|string can be used in concatenation? ( Ignorable by Annotation )

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

46
      $classFullFileName = str_replace('\\', '/', $data[static::P_FOLDER] . /** @scrutinizer ignore-type */ $theClassFile) . DOT_PHP_EX;
Loading history...
47 1
      if(file_exists($classFullFileName) && is_file($classFullFileName)) {
48 1
        require_once($classFullFileName);
49 1
        if(method_exists($class, '_constructorStatic')) {
50
          $class::_constructorStatic();
51
        }
52 1
      }
53 1
    }
54 1
  }
55
56
  /**
57
   * @param string $absoluteClassRoot - absolute path to root class folder
58
   * @param string $classPrefix - PHP class prefix to ignore. Can be whole namespace or part of it
59
   */
60
  public static function register($absoluteClassRoot, $classPrefix = '') {
61
    static::_constructorStatic();
62
63
    $absoluteClassRoot = str_replace('\\', '/', SN_ROOT_PHYSICAL . $absoluteClassRoot);
64
65
    if(!($absoluteClassRoot = realpath($absoluteClassRoot))) {
66
      // TODO - throw new \Exception("There is some error when installing autoloader for '{$absoluteClassRoot}' class prefix '{$classPrefix}'");
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
      return;
68
    }
69
    $absoluteClassRoot = str_replace('\\', '/', $absoluteClassRoot) . '/';
70
71
    if($classPrefix && strrpos($classPrefix, 1) != '\\') {
72
      $classPrefix .= '\\';
73
    }
74
75
    static::$folders[] = [
76
      static::P_FOLDER => $absoluteClassRoot,
77
      static::P_PREFIX => $classPrefix,
78
    ];
79
  }
80
81
//  /**
82
//   * @param string $relativeClassRoot - relative path to root class folder from game root (where index.php lies)
83
//   * @param string $classPrefix - PHP class prefix to ignore. Can be whole namespace or part of it
84
//   */
85
//  public static function registerRelative($relativeClassRoot, $classPrefix = '') {
86
//    static::register(SN_ROOT_PHYSICAL . $relativeClassRoot, $classPrefix);
87
//  }
88
89
  public static function reset() {
90
    static::$folders = [];
91
  }
92
93
}
94