Skip to content

Commit 9127f1f

Browse files
committed
refactoring: extracted base class Member for properties, methods and constants
1 parent ab798cb commit 9127f1f

File tree

4 files changed

+146
-234
lines changed

4 files changed

+146
-234
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function __toString()
119119
$properties = [];
120120
foreach ($this->properties as $property) {
121121
$properties[] = Helpers::formatDocComment($property->getComment())
122-
. $property->getVisibility() . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
122+
. ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
123123
. ($property->value === NULL ? '' : ' = ' . Helpers::dump($property->value))
124124
. ";\n";
125125
}

src/PhpGenerator/Member.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
namespace Nette\PhpGenerator;
9+
10+
use Nette;
11+
12+
13+
/**
14+
* Class member.
15+
*/
16+
abstract class Member
17+
{
18+
use Nette\SmartObject;
19+
20+
/** @var string */
21+
private $name;
22+
23+
/** @var string|NULL public|protected|private */
24+
private $visibility;
25+
26+
/** @var string|NULL */
27+
private $comment;
28+
29+
30+
/**
31+
* @param string
32+
*/
33+
public function __construct($name = '')
34+
{
35+
$this->setName($name);
36+
}
37+
38+
39+
/** @deprecated */
40+
public function setName($name)
41+
{
42+
$this->name = (string) $name;
43+
return $this;
44+
}
45+
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getName()
51+
{
52+
return $this->name;
53+
}
54+
55+
56+
/**
57+
* @param string|NULL public|protected|private
58+
* @return static
59+
*/
60+
public function setVisibility($val)
61+
{
62+
if (!in_array($val, ['public', 'protected', 'private', NULL], TRUE)) {
63+
throw new Nette\InvalidArgumentException('Argument must be public|protected|private.');
64+
}
65+
$this->visibility = $val;
66+
return $this;
67+
}
68+
69+
70+
/**
71+
* @return string|NULL
72+
*/
73+
public function getVisibility()
74+
{
75+
return $this->visibility;
76+
}
77+
78+
79+
/**
80+
* @param string|NULL
81+
* @return static
82+
*/
83+
public function setComment($val)
84+
{
85+
$this->comment = $val ? (string) $val : NULL;
86+
return $this;
87+
}
88+
89+
90+
/**
91+
* @return string|NULL
92+
*/
93+
public function getComment()
94+
{
95+
return $this->comment;
96+
}
97+
98+
99+
/**
100+
* @param string
101+
* @return static
102+
*/
103+
public function addComment($val)
104+
{
105+
$this->comment .= $this->comment ? "\n$val" : $val;
106+
return $this;
107+
}
108+
109+
110+
/** @deprecated */
111+
public function setDocuments(array $s)
112+
{
113+
trigger_error(__METHOD__ . '() is deprecated, use similar setComment()', E_USER_DEPRECATED);
114+
return $this->setComment(implode("\n", $s));
115+
}
116+
117+
118+
/** @deprecated */
119+
public function getDocuments()
120+
{
121+
trigger_error(__METHOD__ . '() is deprecated, use similar getComment()', E_USER_DEPRECATED);
122+
return $this->comment ? [$this->comment] : [];
123+
}
124+
125+
126+
/** @deprecated */
127+
public function addDocument($s)
128+
{
129+
trigger_error(__METHOD__ . '() is deprecated, use addComment()', E_USER_DEPRECATED);
130+
return $this->addComment($s);
131+
}
132+
133+
}

src/PhpGenerator/Method.php

Lines changed: 7 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@
1515
*
1616
* @property string $body
1717
*/
18-
class Method
18+
class Method extends Member
1919
{
20-
use Nette\SmartObject;
21-
22-
/** @var string|NULL */
23-
private $name;
24-
2520
/** @var array of name => Parameter */
2621
private $parameters = [];
2722

@@ -34,9 +29,6 @@ class Method
3429
/** @var bool */
3530
private $static = FALSE;
3631

37-
/** @var string|NULL public|protected|private */
38-
private $visibility;
39-
4032
/** @var bool */
4133
private $final = FALSE;
4234

@@ -49,9 +41,6 @@ class Method
4941
/** @var bool */
5042
private $variadic = FALSE;
5143

52-
/** @var string|NULL */
53-
private $comment;
54-
5544
/** @var PhpNamespace|NULL */
5645
private $namespace;
5746

@@ -82,14 +71,14 @@ public static function from($from)
8271
if ($from instanceof \ReflectionMethod) {
8372
$isInterface = $from->getDeclaringClass()->isInterface();
8473
$method->static = $from->isStatic();
85-
$method->visibility = $from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : ($isInterface ? NULL : 'public'));
74+
$method->setVisibility($from->isPrivate() ? 'private' : ($from->isProtected() ? 'protected' : ($isInterface ? NULL : 'public')));
8675
$method->final = $from->isFinal();
8776
$method->abstract = $from->isAbstract() && !$isInterface;
8877
$method->body = $from->isAbstract() ? FALSE : '';
8978
}
9079
$method->returnReference = $from->returnsReference();
9180
$method->variadic = $from->isVariadic();
92-
$method->comment = Helpers::unformatDocComment($from->getDocComment());
81+
$method->setComment(Helpers::unformatDocComment($from->getDocComment()));
9382
if (PHP_VERSION_ID >= 70000 && $from->hasReturnType()) {
9483
$method->returnType = (string) $from->getReturnType();
9584
$method->returnNullable = $from->getReturnType()->allowsNull();
@@ -127,37 +116,20 @@ public function __toString()
127116
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
128117
}
129118

130-
return Helpers::formatDocComment($this->comment . "\n")
119+
return Helpers::formatDocComment($this->getComment() . "\n")
131120
. ($this->abstract ? 'abstract ' : '')
132121
. ($this->final ? 'final ' : '')
133-
. ($this->visibility ? $this->visibility . ' ' : '')
122+
. ($this->getVisibility() ? $this->getVisibility() . ' ' : '')
134123
. ($this->static ? 'static ' : '')
135124
. 'function '
136125
. ($this->returnReference ? '&' : '')
137-
. $this->name
126+
. $this->getName()
138127
. '(' . implode(', ', $parameters) . ')'
139128
. ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
140129
. ($this->returnType ? ': ' . ($this->returnNullable ? '?' : '')
141130
. ($this->namespace ? $this->namespace->unresolveName($this->returnType) : $this->returnType) : '')
142131
. ($this->abstract || $this->body === FALSE ? ';'
143-
: ($this->name ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
144-
}
145-
146-
147-
/** @deprecated */
148-
public function setName($name)
149-
{
150-
$this->name = $name ? (string) $name : NULL;
151-
return $this;
152-
}
153-
154-
155-
/**
156-
* @return string|NULL
157-
*/
158-
public function getName()
159-
{
160-
return $this->name;
132+
: ($this->getName() ? "\n" : ' ') . "{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
161133
}
162134

163135

@@ -278,29 +250,6 @@ public function isStatic()
278250
}
279251

280252

281-
/**
282-
* @param string|NULL public|protected|private
283-
* @return static
284-
*/
285-
public function setVisibility($val)
286-
{
287-
if (!in_array($val, ['public', 'protected', 'private', NULL], TRUE)) {
288-
throw new Nette\InvalidArgumentException('Argument must be public|protected|private|NULL.');
289-
}
290-
$this->visibility = $val ? (string) $val : NULL;
291-
return $this;
292-
}
293-
294-
295-
/**
296-
* @return string|NULL
297-
*/
298-
public function getVisibility()
299-
{
300-
return $this->visibility;
301-
}
302-
303-
304253
/**
305254
* @param bool
306255
* @return static
@@ -401,61 +350,6 @@ public function isVariadic()
401350
}
402351

403352

404-
/**
405-
* @param string|NULL
406-
* @return static
407-
*/
408-
public function setComment($val)
409-
{
410-
$this->comment = $val ? (string) $val : NULL;
411-
return $this;
412-
}
413-
414-
415-
/**
416-
* @return string|NULL
417-
*/
418-
public function getComment()
419-
{
420-
return $this->comment;
421-
}
422-
423-
424-
/**
425-
* @param string
426-
* @return static
427-
*/
428-
public function addComment($val)
429-
{
430-
$this->comment .= $this->comment ? "\n$val" : $val;
431-
return $this;
432-
}
433-
434-
435-
/** @deprecated */
436-
public function setDocuments(array $s)
437-
{
438-
trigger_error(__METHOD__ . '() is deprecated, use similar setComment()', E_USER_DEPRECATED);
439-
return $this->setComment(implode("\n", $s));
440-
}
441-
442-
443-
/** @deprecated */
444-
public function getDocuments()
445-
{
446-
trigger_error(__METHOD__ . '() is deprecated, use similar getComment()', E_USER_DEPRECATED);
447-
return $this->comment ? [$this->comment] : [];
448-
}
449-
450-
451-
/** @deprecated */
452-
public function addDocument($s)
453-
{
454-
trigger_error(__METHOD__ . '() is deprecated, use addComment()', E_USER_DEPRECATED);
455-
return $this->addComment($s);
456-
}
457-
458-
459353
/**
460354
* @return static
461355
*/

0 commit comments

Comments
 (0)