Skip to content

Commit 65d6e55

Browse files
committed
Add docblocks, improve constructors
1 parent fc48774 commit 65d6e55

File tree

7 files changed

+161
-26
lines changed

7 files changed

+161
-26
lines changed

src/Model/ArgumentModel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class ArgumentModel implements RenderableInterface
2828
/**
2929
* ArgumentModel constructor.
3030
* @param string $name
31+
* @param string|null $type
32+
* @param mixed|null $default
3133
*/
32-
public function __construct($name)
34+
public function __construct($name, $type = null, $default = null)
3335
{
34-
$this->setName($name);
36+
$this->setName($name)
37+
->setType($type)
38+
->setDefault($default);
3539
}
3640

3741
/**

src/Model/ClassModel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Krlove\Generator\Collection\RenderableCollection;
88
use Krlove\Generator\Line;
99
use Krlove\Generator\Line\EmptyLine;
10+
use Krlove\Generator\Model\Traits\DocBlockTrait;
1011
use Krlove\Generator\RenderableInterface;
1112

1213
class ClassModel implements RenderableInterface
1314
{
15+
use DocBlockTrait;
16+
1417
/**
1518
* @var ClassNameModel
1619
*/
@@ -74,6 +77,10 @@ public function render()
7477
$output[] = $this->uses->render();
7578
$output[] = new EmptyLine(2);
7679
}
80+
if ($this->docBlock !== null) {
81+
$output[] = $this->docBlock->render();
82+
$output[] = new EmptyLine();
83+
}
7784
$output[] = $this->name->render();
7885
$output[] = new EmptyLine();
7986
if (!$this->traits->isEmpty()) {

src/Model/ClassNameModel.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,32 @@ class ClassNameModel implements RenderableInterface
3030
/**
3131
* PHPClassName constructor.
3232
* @param string $name
33-
* @param string $extends
33+
* @param string|null $extends
3434
*/
35-
public function __construct($name, $extends)
35+
public function __construct($name, $extends = null)
3636
{
37-
$this->setName($name);
38-
$this->setExtends($extends);
37+
$this->setName($name)
38+
->setExtends($extends);
3939
}
4040

4141
/**
4242
* {@inheritDoc}
4343
*/
4444
public function render()
4545
{
46-
$lines = new LineCollection();
46+
$output = new LineCollection();
4747
$name = sprintf('class %s', $this->name);
4848
if ($this->extends !== null) {
4949
$name .= sprintf(' extends %s', $this->extends);
5050
}
51-
if ($this->implements !== null) {
51+
if (count($this->implements) > 0) {
5252
$name .= sprintf(' implements %s', implode(', ', $this->implements));
5353
}
5454

55-
$lines[] = new Line($name);
56-
$lines[] = new Line('{');
55+
$output[] = new Line($name);
56+
$output[] = new Line('{');
5757

58-
return $lines;
58+
return $output;
5959
}
6060

6161
/**

src/Model/DocBlockModel.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Model;
4+
5+
use Krlove\Generator\Collection\LineCollection;
6+
use Krlove\Generator\RenderableInterface;
7+
8+
/**
9+
* Class DocBlockModel
10+
* @package Krlove\Generator\Model
11+
*/
12+
class DocBlockModel implements RenderableInterface
13+
{
14+
/**
15+
* @var array
16+
*/
17+
protected $content = [];
18+
19+
/**
20+
* DocBlockModel constructor.
21+
*/
22+
public function __construct()
23+
{
24+
$args = func_get_args();
25+
foreach ($args as $arg) {
26+
$this->addContent($arg);
27+
}
28+
}
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function render()
34+
{
35+
$output = new LineCollection();
36+
$output[] = '/**';
37+
if ($this->content) {
38+
foreach ($this->content as $item) {
39+
$output[] = sprintf(' * %s', $item);
40+
}
41+
} else {
42+
$output[] = ' *';
43+
}
44+
$output[] = ' */';
45+
46+
return $output;
47+
}
48+
49+
/**
50+
* @return array
51+
*/
52+
public function getContent()
53+
{
54+
return $this->content;
55+
}
56+
57+
/**
58+
* @param string $content
59+
*
60+
* @return $this
61+
*/
62+
public function addContent($content)
63+
{
64+
$this->content[] = $content;
65+
66+
return $this;
67+
}
68+
}

src/Model/MethodModel.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Krlove\Generator\Collection\LineCollection;
66
use Krlove\Generator\Collection\RenderableCollection;
77
use Krlove\Generator\Line\Line;
8+
use Krlove\Generator\Model\Traits\DocBlockTrait;
89
use Krlove\Generator\Model\Traits\ModifierTrait;
910
use Krlove\Generator\RenderableInterface;
1011

@@ -16,6 +17,7 @@
1617
class MethodModel implements RenderableInterface
1718
{
1819
use ModifierTrait;
20+
use DocBlockTrait;
1921

2022
/**
2123
* @var string
@@ -33,12 +35,14 @@ class MethodModel implements RenderableInterface
3335
protected $body;
3436

3537
/**
36-
* PHPClassMethod constructor.
38+
* MethodModel constructor.
3739
* @param string $name
40+
* @param string $modifier
3841
*/
39-
public function __construct($name)
42+
public function __construct($name, $modifier = 'public')
4043
{
41-
$this->setName($name);
44+
$this->setName($name)
45+
->setModifier($modifier);
4246

4347
$this->arguments = new RenderableCollection();
4448
}
@@ -49,18 +53,21 @@ public function __construct($name)
4953
public function render()
5054
{
5155
$lines = new LineCollection();
52-
$output = sprintf('%s function %s(', $this->modifier, $this->name);
56+
if ($this->docBlock !== null) {
57+
$lines[] = $this->docBlock->render();
58+
}
59+
$function = sprintf('%s function %s(', $this->modifier, $this->name);
5360
if ($this->arguments) {
5461
$arguments = [];
5562
foreach ($this->arguments as $argument) {
5663
$arguments[] = $argument->render();
5764
}
5865

59-
$output .= implode(', ', $arguments);
66+
$function .= implode(', ', $arguments);
6067
}
61-
$output .= ')';
68+
$function .= ')';
6269

63-
$lines[] = new Line($output);
70+
$lines[] = new Line($function);
6471
$lines[] = new Line('{');
6572
if ($this->body) {
6673
$lines[] = new Line($this->body); // todo make body renderable

src/Model/PropertyModel.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,62 @@
22

33
namespace Krlove\Generator\Model;
44

5-
use Krlove\Generator\Line\Line;
5+
use Krlove\Generator\Collection\LineCollection;
6+
use Krlove\Generator\Model\Traits\DocBlockTrait;
67
use Krlove\Generator\Model\Traits\ModifierTrait;
78
use Krlove\Generator\Model\Traits\ValueTrait;
89
use Krlove\Generator\RenderableInterface;
910

1011
/**
1112
* TODO: add support for static and virtual properties
13+
* TODO: add abstract and final modifiers
1214
* Class PHPClassProperty
1315
* @package Krlove\Generator\Model
1416
*/
1517
class PropertyModel implements RenderableInterface
1618
{
1719
use ValueTrait;
1820
use ModifierTrait;
21+
use DocBlockTrait;
1922

2023
/**
2124
* @var string
2225
*/
2326
protected $name;
2427

2528
/**
26-
* PHPClassProperty constructor.
29+
* PropertyModel constructor.
2730
* @param string $name
31+
* @param string $modifier
32+
* @param mixed|null $value
2833
*/
29-
public function __construct($name)
34+
public function __construct($name, $modifier = 'public', $value = null)
3035
{
31-
$this->setName($name);
36+
$this->setName($name)
37+
->setModifier($modifier)
38+
->setValue($value);
3239
}
3340

3441
/**
3542
* {@inheritDoc}
3643
*/
3744
public function render()
3845
{
39-
$output = sprintf('%s $%s', $this->modifier, $this->name);
46+
$lines = new LineCollection();
47+
if ($this->docBlock !== null) {
48+
$lines[] = $this->docBlock->render();
49+
}
50+
$property = sprintf('%s $%s', $this->modifier, $this->name);
4051
if ($this->value !== null) {
4152
$value = $this->renderValue();
4253
if ($value !== null) {
43-
$output .= sprintf(' = %s', $this->renderValue());
54+
$property .= sprintf(' = %s', $this->renderValue());
4455
}
4556
}
46-
$output .= ';';
57+
$property .= ';';
58+
$lines[] = $property;
4759

48-
return new Line($output);
60+
return $lines;
4961
}
5062

5163
/**

src/Model/Traits/DocBlockTrait.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Model\Traits;
4+
5+
use Krlove\Generator\Model\DocBlockModel;
6+
7+
/**
8+
* Class DocBlockTrait
9+
* @package Krlove\Generator\Model\Traits
10+
*/
11+
trait DocBlockTrait
12+
{
13+
/**
14+
* @var DocBlockModel
15+
*/
16+
protected $docBlock;
17+
18+
/**
19+
* @return DocBlockModel
20+
*/
21+
public function getDocBlock()
22+
{
23+
return $this->docBlock;
24+
}
25+
26+
/**
27+
* @param DocBlockModel $docBlock
28+
*
29+
* @return $this
30+
*/
31+
public function setDocBlock($docBlock)
32+
{
33+
$this->docBlock = $docBlock;
34+
35+
return $this;
36+
}
37+
}

0 commit comments

Comments
 (0)