Skip to content

Commit 27d6e1f

Browse files
committed
Add new tests and update documentation
- Add new tests - Finish first version of the documentation - Move all components to the top folder
1 parent 90bc06d commit 27d6e1f

File tree

13 files changed

+763
-43
lines changed

13 files changed

+763
-43
lines changed

readme.md

Lines changed: 438 additions & 18 deletions
Large diffs are not rendered by default.

src/ArrowFunction.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@ class ArrowFunction extends AbstractFunction
99
/** @var GeneratorInterface|string */
1010
private $expression;
1111

12-
public function __construct($expression = '', string $returnType = '')
12+
public function __construct($expression = null, string $returnType = '')
1313
{
1414
$this->signature = new Signature('', Modifier::NONE, $returnType, 'fn');
1515
$this->expression = $this->manageExprDependency($expression);
1616

1717
$this->dependencyAwareChildren[] = $this->signature;
1818
}
1919

20-
public static function new($expression = '', string $returnType = '')
20+
public static function new($expression = null, string $returnType = '')
2121
{
2222
return new self($expression, $returnType);
2323
}
2424

2525
public function generate(): string
2626
{
27-
return "$this->signature => $this->expression";
27+
$expression = Utils::stringify($this->expression);
28+
29+
return "$this->signature => $expression";
2830
}
2931

3032
/**
@@ -36,7 +38,7 @@ public function getExpression()
3638
}
3739

3840
/**
39-
* @param GeneratorInterface|string $expression
41+
* @param mixed $expression
4042
*/
4143
public function setExpression($expression): self
4244
{
@@ -49,7 +51,7 @@ protected function manageExprDependency($value)
4951
{
5052
if ($value instanceof DependencyAwareGenerator) {
5153
$this->dependencyAwareChildren['expr'] = $value;
52-
} elseif (is_string($value)) {
54+
} else {
5355
unset($this->dependencyAwareChildren['expr']);
5456
}
5557

src/Collection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ public function push($item): self
207207
{
208208
$this->items[] = $item;
209209

210+
if ($item instanceof DependencyAwareGenerator) {
211+
$this->dependencyAwareChildren[] = $item;
212+
}
213+
210214
return $this;
211215
}
212216
}

src/Func.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Murtukov\PHPCodeGenerator;
6+
7+
class Func extends AbstractFunction implements BlockInterface
8+
{
9+
use ScopedContentTrait, DocBlockTrait;
10+
11+
public function __construct(string $name, string $returnType = '')
12+
{
13+
$this->signature = new Signature($name, Modifier::NONE, $returnType);
14+
$this->dependencyAwareChildren = [$this->signature];
15+
}
16+
17+
public static function new(string $name, string $returnType = ''): self
18+
{
19+
return new static($name, $returnType);
20+
}
21+
22+
public function generate(): string
23+
{
24+
return <<<CODE
25+
{$this->buildDocBlock()}{$this->signature->generate(false)}
26+
{
27+
{$this->generateContent()}
28+
}
29+
CODE;
30+
}
31+
}

src/ControlStructures/IfElse.php renamed to src/IfElse.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Murtukov\PHPCodeGenerator\ControlStructures;
6-
7-
use Murtukov\PHPCodeGenerator\AbstractGenerator;
8-
use Murtukov\PHPCodeGenerator\BlockInterface;
9-
use Murtukov\PHPCodeGenerator\DependencyAwareGenerator;
10-
use Murtukov\PHPCodeGenerator\GeneratorInterface;
11-
use Murtukov\PHPCodeGenerator\ScopedContentTrait;
5+
namespace Murtukov\PHPCodeGenerator;
126

137
class IfElse extends AbstractGenerator implements BlockInterface
148
{
@@ -30,7 +24,7 @@ public function __construct($ifExpression = '')
3024
$this->expression = $ifExpression;
3125
}
3226

33-
public static function create($ifExpression = ''): self
27+
public static function new($ifExpression = ''): self
3428
{
3529
return new self($ifExpression);
3630
}

src/Literal.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public function __construct(string $value)
1313
$this->value = $value;
1414
}
1515

16+
public static function new(string $value)
17+
{
18+
return new static($value);
19+
}
20+
1621
public function generate(): string
1722
{
1823
return $this->value;

src/ControlStructures/Loop.php renamed to src/Loop.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Murtukov\PHPCodeGenerator\ControlStructures;
6-
7-
use Murtukov\PHPCodeGenerator\BlockInterface;
8-
use Murtukov\PHPCodeGenerator\DependencyAwareGenerator;
9-
use Murtukov\PHPCodeGenerator\ScopedContentTrait;
5+
namespace Murtukov\PHPCodeGenerator;
106

117
class Loop extends DependencyAwareGenerator implements BlockInterface
128
{

tests/ArrowFunctionTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Murtukov\PHPCodeGenerator\ArrowFunction;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ArrowFunctionTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
public function emptyBody()
14+
{
15+
$arrow = ArrowFunction::new();
16+
17+
$this->expectOutputString(<<<CODE
18+
fn() => null
19+
CODE);
20+
21+
echo $arrow;
22+
23+
return $arrow;
24+
}
25+
26+
/**
27+
* @test
28+
* @depends emptyBody
29+
*/
30+
public function setExpression(ArrowFunction $arrow)
31+
{
32+
$innerArrow = ArrowFunction::new([
33+
'name' => 'Alrik',
34+
'age' => 30
35+
]);
36+
37+
$arrow->setExpression($innerArrow);
38+
39+
$template = <<<CODE
40+
fn() => fn() => [
41+
'name' => 'Alrik',
42+
'age' => 30,
43+
]
44+
CODE;
45+
46+
$this->expectOutputString($template);
47+
48+
echo $arrow;
49+
50+
return [$arrow, $template];
51+
}
52+
53+
/**
54+
* @test
55+
* @depends setExpression
56+
*/
57+
public function setStatic(array $values)
58+
{
59+
[$arrow, $template] = $values;
60+
61+
$arrow->setStatic();
62+
$this->expectOutputString('static '.$template);
63+
64+
echo $arrow;
65+
}
66+
}

tests/ClosureTest.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Murtukov\PHPCodeGenerator\Argument;
6+
use Murtukov\PHPCodeGenerator\Closure;
7+
use Murtukov\PHPCodeGenerator\Loop;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ClosureTest extends TestCase
11+
{
12+
/**
13+
* @test
14+
*/
15+
public function emptyBase()
16+
{
17+
$closure = Closure::new('array');
18+
19+
$this->expectOutputString(<<<CODE
20+
function (): array {
21+
22+
}
23+
CODE);
24+
25+
echo $closure;
26+
27+
return $closure;
28+
}
29+
30+
/**
31+
* @test
32+
* @depends emptyBase
33+
*/
34+
public function addArguments(CLosure $closure)
35+
{
36+
$closure->addArgument('value');
37+
38+
$closure->createArgument('options')
39+
->setType('array')
40+
->setDefaultValue([]);
41+
42+
$arg = Argument::new('filter', 'bool', false);
43+
44+
$closure->add($arg);
45+
46+
$this->expectOutputString(<<<CODE
47+
function (\$value, array \$options = [], bool \$filter = false): array {
48+
49+
}
50+
CODE);
51+
52+
echo $closure;
53+
54+
return $closure;
55+
}
56+
57+
/**
58+
* @test
59+
* @depends addArguments
60+
*/
61+
public function bindVars(Closure $closure)
62+
{
63+
$closure->bindVar('this');
64+
$closure->bindVar('global', true);
65+
66+
$this->expectOutputString(<<<CODE
67+
function (\$value, array \$options = [], bool \$filter = false) use (\$this, &\$global): array {
68+
69+
}
70+
CODE);
71+
72+
echo $closure;
73+
74+
return $closure;
75+
}
76+
77+
/**
78+
* @test
79+
* @depends bindVars
80+
*/
81+
public function addContent(Closure $closure)
82+
{
83+
$foreach = Loop::foreach('$options as &$option')
84+
->append('unset($option)');
85+
86+
$closure->append($foreach);
87+
88+
$template = <<<CODE
89+
function (\$value, array \$options = [], bool \$filter = false) use (\$this, &\$global): array {
90+
foreach (\$options as &\$option) {
91+
unset(\$option);
92+
}
93+
}
94+
CODE;
95+
96+
$this->expectOutputString($template);
97+
98+
echo $closure;
99+
100+
return [$closure, $template];
101+
}
102+
103+
/**
104+
* @test
105+
* @depends addContent
106+
*/
107+
public function setStatic($vals)
108+
{
109+
[$closure, $template] = $vals;
110+
111+
$closure->setStatic();
112+
113+
$this->expectOutputString('static '.$template);
114+
115+
echo $closure;
116+
}
117+
}

tests/FuncTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Murtukov\PHPCodeGenerator\Argument;
6+
use Murtukov\PHPCodeGenerator\Func;
7+
use Murtukov\PHPCodeGenerator\Instance;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class FuncTest extends TestCase
11+
{
12+
/**
13+
* @test
14+
*/
15+
public function emptyBase()
16+
{
17+
$func = Func::new('myMethod', 'void');
18+
19+
$this->expectOutputString(<<<CODE
20+
function myMethod(): void
21+
{
22+
23+
}
24+
CODE);
25+
26+
echo $func;
27+
28+
return $func;
29+
}
30+
31+
/**
32+
* @test
33+
* @depends emptyBase
34+
*/
35+
public function addContent(Func $func)
36+
{
37+
$func->append('$object = ', Instance::new(stdClass::class));
38+
39+
$this->expectOutputString(<<<CODE
40+
function myMethod(): void
41+
{
42+
\$object = new stdClass();
43+
}
44+
CODE);
45+
46+
echo $func;
47+
48+
return $func;
49+
}
50+
51+
/**
52+
* @test
53+
* @depends addContent
54+
*/
55+
public function addArguments(Func $func)
56+
{
57+
$func->createArgument('arg1', SplHeap::class, null)->setNullable();
58+
$func->createArgument('arg2', 'string', '');
59+
$func->add(Argument::new('arg3'));
60+
61+
$this->expectOutputString(<<<CODE
62+
function myMethod(?SplHeap \$arg1 = null, string \$arg2 = '', \$arg3): void
63+
{
64+
\$object = new stdClass();
65+
}
66+
CODE);
67+
68+
echo $func;
69+
70+
return $func;
71+
}
72+
}

0 commit comments

Comments
 (0)