Issues (16)

src/PhpFile.php (1 issue)

Severity
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder;
4
5
use PhpLang\ScopeExit;
6
7
class PhpFile extends PhpTemplate
8
{
9
    /** @var string */
10
    private $namespace;
11
    /** @var PhpNamespaces */
12
    private $namespaces;
13
    /** @var PhpCode */
14
    private $code;
15
16
    /** @var string */
17
    private $comment = <<<COMMENT
18
ATTENTION!!! The code below was carefully crafted by a mean machine.
19
Please consider to NOT put any emotional human-generated modifications as the splendid AI will throw them away with no mercy.
20
COMMENT;
21
22 5
    public function __construct($namespace = null)
23
    {
24 5
        $this->namespace = $namespace;
25 5
        $this->namespaces = new PhpNamespaces();
26 5
        $this->code = new PhpCode();
27 5
    }
28
29
    /**
30
     * @return PhpNamespaces
31
     */
32 5
    public function getNamespaces()
33
    {
34 5
        return $this->namespaces;
35
    }
36
37 5
    protected function toString()
38
    {
39 5
        $prev = self::setCurrentPhpFile($this);
40
        /** @noinspection PhpUnusedLocalVariableInspection */
41 5
        $_ = new ScopeExit(function () use ($prev) {
0 ignored issues
show
The assignment to $_ is dead and can be removed.
Loading history...
42 5
            self::setCurrentPhpFile($prev);
43 5
        });
44
45 5
        $this->namespaces->setFileNamespace($this->namespace);
46 5
        $code = $this->code->render();
47 5
        $phpDoc = new PhpDoc();
48 5
        $phpDoc->add(PhpDoc::TAG_FILE, $this->comment);
49 5
        $phpDoc = $phpDoc->render();
50
51
        $result = <<<PHP
52
<?php
53 5
{$phpDoc}
54 5
{$this->renderNamespace()}{$this->namespaces}
55
56 5
{$code}
57
PHP;
58 5
        return $result;
59
60
    }
61
62 5
    private function renderNamespace()
63
    {
64 5
        return $this->namespace
65 4
            ? "namespace " . trim($this->namespace, '\\') . ";\n\n"
66 5
            : '';
67
68
    }
69
70
    /** @var null|PhpFile */
71
    private static $currentPhpFile;
72
73
    /**
74
     * @return null|PhpFile
75
     */
76 12
    public static function getCurrentPhpFile()
77
    {
78 12
        return self::$currentPhpFile;
79
    }
80
81
    /**
82
     * @param null|PhpFile $currentPhpFile
83
     * @return null|PhpFile previous php file
84
     */
85 5
    public static function setCurrentPhpFile($currentPhpFile)
86
    {
87 5
        $previous = self::$currentPhpFile;
88 5
        self::$currentPhpFile = $currentPhpFile;
89 5
        return $previous;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getNamespace()
96
    {
97
        return $this->namespace;
98
    }
99
100
    /**
101
     * @param string $namespace
102
     * @return PhpFile
103
     */
104 4
    public function setNamespace($namespace)
105
    {
106 4
        $this->namespace = $namespace;
107 4
        return $this;
108
    }
109
110
    /**
111
     * @return PhpCode
112
     */
113 5
    public function getCode()
114
    {
115 5
        return $this->code;
116
    }
117
118
    /**
119
     * @param PhpCode $code
120
     * @return PhpFile
121
     */
122
    public function setCode($code)
123
    {
124
        $this->code = $code;
125
        return $this;
126
    }
127
}