GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 349a12...725c6d )
by joseph
15:11 queued 12:41
created

TestCodeGenerator.php$0 ➔ extendAutoloader()   A

Complexity

Conditions 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TestCodeGenerator.php$0 ➔ loadClass() 0 12 4
A TestCodeGenerator.php$0 ➔ __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Assets;
4
5
use Composer\Autoload\ClassLoader;
6
use EdmondsCommerce\DoctrineStaticMeta\Builder\Builder;
7
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\Tests\Large\FullProjectBuildLargeTest;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
class TestCodeGenerator
12
{
13
    public const TEST_PROJECT_ROOT_NAMESPACE = FullProjectBuildLargeTest::TEST_PROJECT_ROOT_NAMESPACE;
14
    public const TEST_ENTITIES               = FullProjectBuildLargeTest::TEST_ENTITIES;
15
    public const TEST_RELATIONS              = FullProjectBuildLargeTest::TEST_RELATIONS;
16
    public const TEST_FIELD_FQN_BASE         = FullProjectBuildLargeTest::TEST_FIELD_NAMESPACE_BASE . '\\Traits';
17
    public const BUILD_DIR                   = AbstractTest::VAR_PATH . '/testCode';
18
19
    /**
20
     * @var Builder
21
     */
22
    protected $builder;
23
    /**
24
     * @var Filesystem
25
     */
26
    protected $filesystem;
27
28
    public function __construct(Builder $builder, Filesystem $filesystem)
29
    {
30
        $this->filesystem = $filesystem;
31
        $this->initBuildDir();
32
        $this->builder = $builder->setProjectRootNamespace(self::TEST_PROJECT_ROOT_NAMESPACE)
33
                                 ->setPathToProjectRoot(self::BUILD_DIR);
34
        $this->buildOnce();
35
    }
36
37
    private function initBuildDir(): void
38
    {
39
40
        if (!is_dir(self::BUILD_DIR)) {
41
            $this->filesystem->mkdir(self::BUILD_DIR);
42
        }
43
    }
44
45
    private function isBuilt(): bool
46
    {
47
        return is_dir(self::BUILD_DIR . '/src');
48
    }
49
50
    public function copyTo(string $destinationPath): void
51
    {
52
        $this->filesystem->mirror(self::BUILD_DIR, $destinationPath);
53
    }
54
55
    public function buildOnce(): void
56
    {
57
        if ($this->isBuilt()) {
58
            return;
59
        }
60
        $this->extendAutoloader();
61
        $entityGenerator    = $this->builder->getEntityGenerator();
62
        $fieldGenerator     = $this->builder->getFieldGenerator();
63
        $fieldSetter        = $this->builder->getFieldSetter();
64
        $relationsGenerator = $this->builder->getRelationsGenerator();
65
        $fields             = [];
66
        foreach (MappingHelper::COMMON_TYPES as $type) {
67
            $fields[] = $fieldGenerator->generateField(
68
                self::TEST_FIELD_FQN_BASE . '\\' . ucwords($type),
69
                $type
70
            );
71
        }
72
        foreach (self::TEST_ENTITIES as $entityFqn) {
73
            $entityGenerator->generateEntity($entityFqn);
74
            foreach ($fields as $fieldFqn) {
75
                $fieldSetter->setEntityHasField($entityFqn, $fieldFqn);
76
            }
77
        }
78
        foreach (self::TEST_RELATIONS as $relation) {
79
            $relationsGenerator->setEntityHasRelationToEntity(...$relation);
80
        }
81
        $this->resetAutoloader();
82
    }
83
84
    private function extendAutoloader(): void
85
    {
86
        $testLoader = new class(self::TEST_PROJECT_ROOT_NAMESPACE) extends ClassLoader
87
        {
88
            /**
89
             * @var string
90
             */
91
            protected $namespace;
92
93
            public function __construct(string $namespace)
94
            {
95
                $this->namespace = $namespace;
96
            }
97
98
            public function loadClass($class)
99
            {
100
                if (false === strpos($class, $this->namespace)) {
101
                    return false;
102
                }
103
                $found = parent::loadClass($class);
104
                if (false === $found || null === $found) {
0 ignored issues
show
introduced by
The condition null === $found is always false.
Loading history...
105
                    //good point to set a breakpoint
106
                    return $found;
107
                }
108
109
                return $found;
110
            }
111
        };
112
        $testLoader->addPsr4(self::TEST_PROJECT_ROOT_NAMESPACE . '\\', self::BUILD_DIR . '/src', true);
113
        $testLoader->addPsr4(self::TEST_PROJECT_ROOT_NAMESPACE . '\\', self::BUILD_DIR . '/tests', true);
114
        $testLoader->register();
115
    }
116
117
    private function resetAutoloader(): void
118
    {
119
        $registered = \spl_autoload_functions();
120
        $loader     = array_pop($registered);
121
        \spl_autoload_unregister($loader);
122
    }
123
}
124