TraitCreator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 22 2
A __destruct() 0 3 1
A __construct() 0 4 1
A addCreator() 0 3 1
A addAlias() 0 3 1
A write() 0 15 1
1
<?php
2
/*
3
 * Copyright (c) Manero Contributors. All rights reserved.
4
 *
5
 * Licensed under the MIT License. See LICENSE.md file in the
6
 * project root for full license information.
7
 */
8
9
namespace Manero\Creator;
10
11
class TraitCreator implements Writeable
12
{
13
    private $handle;
14
15
    private $creators;
16
17
    public function __construct(string $file)
18
    {
19
        $this->handle = fopen($file, 'w');
20
        $this->creators = [];
21
    }
22
    public function create() : void
23
    {
24
        $preBeanTemplate = '<?php
25
        %%
26
        %%use bitExpert\Disco\BeanFactoryRegistry;
27
        %%use bitExpert\Disco\Annotations\Alias;
28
        %%use bitExpert\Disco\Annotations\Bean;
29
        %%use bitExpert\Disco\Annotations\Parameter;
30
        %%
31
        %%trait ManeroConfigTrait
32
        %%{';
33
34
        $postBeanTemplate = '
35
        %%}
36
        %%';
37
38
        $this->write($preBeanTemplate);
39
        /** @var \Manero\Creator\Createable $creator */
40
        foreach ($this->creators as $creator) {
41
            $creator->create();
42
        }
43
        $this->write($postBeanTemplate);
44
    }
45
46
    public function addCreator(Createable $creator) : void
47
    {
48
        $this->creators[$creator->getClass()] = $creator;
49
    }
50
51
    public function write(string $string) : void
52
    {
53
        $string = preg_replace(
54
            [
55
                '/[ \t]+%%/',
56
                '/[ \t]+%indent%/',
57
            ],
58
            [
59
                '',
60
                '    ',
61
            ],
62
            $string
63
        );
64
65
        fwrite($this->handle, $string);
0 ignored issues
show
Bug introduced by
It seems like $this->handle can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, 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

65
        fwrite(/** @scrutinizer ignore-type */ $this->handle, $string);
Loading history...
66
    }
67
68
    public function __destruct()
69
    {
70
        fclose($this->handle);
0 ignored issues
show
Bug introduced by
It seems like $this->handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

70
        fclose(/** @scrutinizer ignore-type */ $this->handle);
Loading history...
71
    }
72
73
    public function addAlias(string $alias, string $class) : void
74
    {
75
        $this->creators[$class]->addAlias($alias);
76
    }
77
}
78