1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\DI\Builder; |
19
|
|
|
|
20
|
|
|
use PhpParser\PrettyPrinter\Standard; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A custom Php-Parser printer. |
24
|
|
|
* |
25
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class CodePrinter extends Standard |
28
|
|
|
{ |
29
|
|
|
public const COMMENT = <<<'COMMENT' |
30
|
|
|
/** |
31
|
|
|
* @internal This class has been auto-generated by the Rade DI. |
32
|
|
|
*/ |
33
|
|
|
COMMENT; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Pretty print nodes. |
37
|
|
|
* |
38
|
|
|
* @param \PhpParser\Node[] $stmts |
39
|
|
|
* @param array<string,mixed> $options |
40
|
|
|
*/ |
41
|
15 |
|
public static function print(array $stmts, array $options = []): string |
42
|
|
|
{ |
43
|
15 |
|
$printer = new self(['shortArraySyntax' => $options['shortArraySyntax'] ??= true]); |
44
|
15 |
|
$spacing = "{\n" . $nl = str_repeat(' ', $options['spacingLevel'] ?? 8) . "\n"; // Replace tabs with spacing |
45
|
|
|
|
46
|
|
|
// Resolve whitespace ... |
47
|
15 |
|
return \str_replace([$spacing, "\n\n}", $nl], ["{\n", "\n}\n", "\n"], $printer->prettyPrintFile($stmts)); |
48
|
|
|
} |
49
|
|
|
|
50
|
14 |
|
protected function pStmt_Return(\PhpParser\Node\Stmt\Return_ $node): string |
51
|
|
|
{ |
52
|
14 |
|
return $this->nl . parent::pStmt_Return($node); |
53
|
|
|
} |
54
|
|
|
|
55
|
13 |
|
protected function pStmt_Declare(\PhpParser\Node\Stmt\Declare_ $node): string |
56
|
|
|
{ |
57
|
13 |
|
return parent::pStmt_Declare($node) . $this->nl; |
58
|
|
|
} |
59
|
|
|
|
60
|
12 |
|
protected function pStmt_Property(\PhpParser\Node\Stmt\Property $node): string |
61
|
|
|
{ |
62
|
12 |
|
return parent::pStmt_Property($node) . "\n"; |
63
|
|
|
} |
64
|
|
|
|
65
|
14 |
|
protected function pStmt_ClassMethod(\PhpParser\Node\Stmt\ClassMethod $node): string |
66
|
|
|
{ |
67
|
14 |
|
$classMethod = parent::pStmt_ClassMethod($node); |
68
|
|
|
|
69
|
14 |
|
if (null !== $node->returnType) { |
70
|
14 |
|
$classMethod = \str_replace(') :', '):', $classMethod); |
71
|
|
|
} |
72
|
|
|
|
73
|
14 |
|
return $classMethod . "\n"; // prefer spaces instead of tab |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|