Skip to content

Commit e58789d

Browse files
committed
refactoring: transformed to traits, removed Member
1 parent 81b2937 commit e58789d

File tree

10 files changed

+318
-384
lines changed

10 files changed

+318
-384
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class ClassType
2121
{
2222
use Nette\SmartObject;
23+
use Traits\CommentAware;
2324

2425
const TYPE_CLASS = 'class';
2526

@@ -51,9 +52,6 @@ class ClassType
5152
/** @var string[] */
5253
private $traits = [];
5354

54-
/** @var string|NULL */
55-
private $comment;
56-
5755
/** @var Constant[] name => Constant */
5856
private $consts = [];
5957

@@ -317,61 +315,6 @@ public function addTrait($trait)
317315
}
318316

319317

320-
/**
321-
* @param string|NULL
322-
* @return static
323-
*/
324-
public function setComment($val)
325-
{
326-
$this->comment = $val ? (string) $val : NULL;
327-
return $this;
328-
}
329-
330-
331-
/**
332-
* @return string|NULL
333-
*/
334-
public function getComment()
335-
{
336-
return $this->comment;
337-
}
338-
339-
340-
/**
341-
* @param string
342-
* @return static
343-
*/
344-
public function addComment($val)
345-
{
346-
$this->comment .= $this->comment ? "\n$val" : $val;
347-
return $this;
348-
}
349-
350-
351-
/** @deprecated */
352-
public function setDocuments(array $s)
353-
{
354-
trigger_error(__METHOD__ . '() is deprecated, use similar setComment()', E_USER_DEPRECATED);
355-
return $this->setComment(implode("\n", $s));
356-
}
357-
358-
359-
/** @deprecated */
360-
public function getDocuments()
361-
{
362-
trigger_error(__METHOD__ . '() is deprecated, use similar getComment()', E_USER_DEPRECATED);
363-
return $this->comment ? [$this->comment] : [];
364-
}
365-
366-
367-
/** @deprecated */
368-
public function addDocument($s)
369-
{
370-
trigger_error(__METHOD__ . '() is deprecated, use addComment()', E_USER_DEPRECATED);
371-
return $this->addComment($s);
372-
}
373-
374-
375318
/**
376319
* @deprecated use setConstants()
377320
* @return static

src/PhpGenerator/Constant.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
/**
1414
* Class constant.
1515
*/
16-
class Constant extends Member
16+
class Constant
1717
{
18+
use Nette\SmartObject;
19+
use Traits\NameAware;
20+
use Traits\VisibilityAware;
21+
use Traits\CommentAware;
22+
1823
/** @var mixed */
1924
private $value;
2025

src/PhpGenerator/Method.php

Lines changed: 12 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
*
1616
* @property string|FALSE $body
1717
*/
18-
class Method extends Member
18+
class Method
1919
{
20-
/** @var array of name => Parameter */
21-
private $parameters = [];
20+
use Nette\SmartObject;
21+
use Traits\FunctionLike;
22+
use Traits\NameAware;
23+
use Traits\VisibilityAware;
24+
use Traits\CommentAware;
2225

2326
/** @var Parameter[] */
2427
private $uses = [];
@@ -35,21 +38,6 @@ class Method extends Member
3538
/** @var bool */
3639
private $abstract = FALSE;
3740

38-
/** @var bool */
39-
private $returnReference = FALSE;
40-
41-
/** @var bool */
42-
private $variadic = FALSE;
43-
44-
/** @var PhpNamespace|NULL */
45-
private $namespace;
46-
47-
/** @var string|NULL */
48-
private $returnType;
49-
50-
/** @var bool */
51-
private $returnNullable;
52-
5341

5442
/**
5543
* @param callable
@@ -63,89 +51,28 @@ public static function from($method)
6351
}
6452

6553

66-
/**
67-
* @param string|NULL
68-
*/
69-
public function __construct($name = NULL)
70-
{
71-
$this->setName($name);
72-
}
73-
74-
7554
/**
7655
* @return string PHP code
7756
*/
7857
public function __toString()
7958
{
80-
$parameters = [];
81-
foreach ($this->parameters as $param) {
82-
$variadic = $this->variadic && $param === end($this->parameters);
83-
$hint = $param->getTypeHint();
84-
$parameters[] = ($hint ? ($param->isNullable() ? '?' : '') . ($this->namespace ? $this->namespace->unresolveName($hint) : $hint) . ' ' : '')
85-
. ($param->isReference() ? '&' : '')
86-
. ($variadic ? '...' : '')
87-
. '$' . $param->getName()
88-
. ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
89-
}
9059
$uses = [];
9160
foreach ($this->uses as $param) {
9261
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
9362
}
94-
95-
return Helpers::formatDocComment($this->getComment() . "\n")
63+
return Helpers::formatDocComment($this->comment . "\n")
9664
. ($this->abstract ? 'abstract ' : '')
9765
. ($this->final ? 'final ' : '')
98-
. ($this->getVisibility() ? $this->getVisibility() . ' ' : '')
66+
. ($this->visibility ? $this->visibility . ' ' : '')
9967
. ($this->static ? 'static ' : '')
10068
. 'function '
10169
. ($this->returnReference ? '&' : '')
102-
. $this->getName()
103-
. '(' . implode(', ', $parameters) . ')'
70+
. $this->name
71+
. $this->parametersToString()
10472
. ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
105-
. ($this->returnType ? ': ' . ($this->returnNullable ? '?' : '')
106-
. ($this->namespace ? $this->namespace->unresolveName($this->returnType) : $this->returnType) : '')
73+
. $this->returnTypeToString()
10774
. ($this->abstract || $this->body === FALSE ? ';'
108-
: ($this->getName() ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
109-
}
110-
111-
112-
/**
113-
* @param Parameter[]
114-
* @return static
115-
*/
116-
public function setParameters(array $val)
117-
{
118-
$this->parameters = [];
119-
foreach ($val as $v) {
120-
if (!$v instanceof Parameter) {
121-
throw new Nette\InvalidArgumentException('Argument must be Nette\PhpGenerator\Parameter[].');
122-
}
123-
$this->parameters[$v->getName()] = $v;
124-
}
125-
return $this;
126-
}
127-
128-
129-
/**
130-
* @return Parameter[]
131-
*/
132-
public function getParameters()
133-
{
134-
return $this->parameters;
135-
}
136-
137-
138-
/**
139-
* @param string without $
140-
* @return Parameter
141-
*/
142-
public function addParameter($name, $defaultValue = NULL)
143-
{
144-
$param = new Parameter($name);
145-
if (func_num_args() > 1) {
146-
$param->setOptional(TRUE)->setDefaultValue($defaultValue);
147-
}
148-
return $this->parameters[$name] = $param;
75+
: ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
14976
}
15077

15178

@@ -267,94 +194,4 @@ public function isAbstract()
267194
return $this->abstract;
268195
}
269196

270-
271-
/**
272-
* @param bool
273-
* @return static
274-
*/
275-
public function setReturnReference($val)
276-
{
277-
$this->returnReference = (bool) $val;
278-
return $this;
279-
}
280-
281-
282-
/**
283-
* @return bool
284-
*/
285-
public function getReturnReference()
286-
{
287-
return $this->returnReference;
288-
}
289-
290-
291-
/**
292-
* @param bool
293-
* @return static
294-
*/
295-
public function setReturnNullable($val)
296-
{
297-
$this->returnNullable = (bool) $val;
298-
return $this;
299-
}
300-
301-
302-
/**
303-
* @return bool
304-
*/
305-
public function getReturnNullable()
306-
{
307-
return $this->returnNullable;
308-
}
309-
310-
311-
/**
312-
* @param bool
313-
* @return static
314-
*/
315-
public function setVariadic($val)
316-
{
317-
$this->variadic = (bool) $val;
318-
return $this;
319-
}
320-
321-
322-
/**
323-
* @return bool
324-
*/
325-
public function isVariadic()
326-
{
327-
return $this->variadic;
328-
}
329-
330-
331-
/**
332-
* @return static
333-
*/
334-
public function setNamespace(PhpNamespace $val = NULL)
335-
{
336-
$this->namespace = $val;
337-
return $this;
338-
}
339-
340-
341-
/**
342-
* @param string|NULL
343-
* @return static
344-
*/
345-
public function setReturnType($val)
346-
{
347-
$this->returnType = $val ? (string) $val : NULL;
348-
return $this;
349-
}
350-
351-
352-
/**
353-
* @return string|NULL
354-
*/
355-
public function getReturnType()
356-
{
357-
return $this->returnType;
358-
}
359-
360197
}

src/PhpGenerator/Parameter.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
class Parameter
1717
{
1818
use Nette\SmartObject;
19-
20-
/** @var string */
21-
private $name = '';
19+
use Traits\NameAware;
2220

2321
/** @var bool */
2422
private $reference = FALSE;
@@ -46,32 +44,6 @@ public static function from(\ReflectionParameter $from)
4644
}
4745

4846

49-
/**
50-
* @param string without $
51-
*/
52-
public function __construct($name = '')
53-
{
54-
$this->setName($name);
55-
}
56-
57-
58-
/** @deprecated */
59-
public function setName($name)
60-
{
61-
$this->name = (string) $name;
62-
return $this;
63-
}
64-
65-
66-
/**
67-
* @return string
68-
*/
69-
public function getName()
70-
{
71-
return $this->name;
72-
}
73-
74-
7547
/**
7648
* @param bool
7749
* @return static

0 commit comments

Comments
 (0)