Bootstrap::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @link    https://github.com/nnx-framework/entry-name-resolver
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\EntryNameResolver\PhpUnit\Test;
7
8
use Zend\Loader\AutoloaderFactory;
9
use Zend\Loader\StandardAutoloader;
10
use RuntimeException;
11
12
error_reporting(E_ALL | E_STRICT);
13
chdir(__DIR__);
14
15
/**
16
 * Class Bootstrap
17
 *
18
 * @package Nnx\EntryNameResolver\PhpUnit\Test
19
 */
20
class Bootstrap
21
{
22
    /**
23
     * Настройка тестов
24
     *
25
     * @throws \RuntimeException
26
     */
27
    public static function init()
28
    {
29
        static::initAutoloader();
30
    }
31
32
33
    /**
34
     * Инициализация автозагрузчика
35
     *
36
     * @return void
37
     *
38
     * @throws RuntimeException
39
     */
40
    protected static function initAutoloader()
41
    {
42
        $vendorPath = static::findParentPath('vendor');
43
        if (is_readable($vendorPath . '/autoload.php')) {
44
45
            /** @noinspection PhpIncludeInspection */
46
            include $vendorPath . '/autoload.php';
47
        }
48
49
        if (!class_exists(AutoloaderFactory::class)) {
50
            $errMsg = sprintf('Error init autoloader. Autoloader class %s not exists', AutoloaderFactory::class);
51
            throw new RuntimeException($errMsg);
52
        }
53
54
        try {
55
            AutoloaderFactory::factory([
56
                StandardAutoloader::class => [
57
                    'autoregister_zf' => true,
58
                    'namespaces' => [
59
                        'Nnx\\EntryNameResolver' => __DIR__ . '/../../src/',
60
                        __NAMESPACE__ => __DIR__ . '/tests/',
61
                        'Nnx\\EntryNameResolver\\PhpUnit\\TestData' => __DIR__ . '/files'
62
                    ]
63
                ]
64
            ]);
65
        } catch (\Exception $e) {
66
            $errMsg = 'Error init autoloader';
67
            throw new RuntimeException($errMsg, $e->getCode(), $e);
68
        }
69
    }
70
71
    /**
72
     * @param $path
73
     *
74
     * @return bool|string
75
     */
76
    protected static function findParentPath($path)
77
    {
78
        $dir = __DIR__;
79
        $previousDir = '.';
80
        while (!is_dir($dir . '/' . $path)) {
81
            $dir = dirname($dir);
82
            if ($previousDir === $dir) {
83
                return false;
84
            }
85
            $previousDir = $dir;
86
        }
87
        return $dir . '/' . $path;
88
    }
89
}
90
91
Bootstrap::init();
92