Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit e8e2e50

Browse files
committed
Merge branch 'develop'
2 parents c6a4f08 + f60a882 commit e8e2e50

16 files changed

+424
-34
lines changed

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 3.4.0 - TBD
6+
7+
### Added
8+
9+
- [#170](https://github.com/zendframework/zend-code/pull/170) adds class constant visibility modifiers support.
10+
11+
- [#169](https://github.com/zendframework/zend-code/pull/169) adds the ability to define declare statements.
12+
13+
- [#167](https://github.com/zendframework/zend-code/pull/167) adds the ability to remove doc block of a member.
14+
15+
### Changed
16+
17+
- [#166](https://github.com/zendframework/zend-code/pull/166) changes omitting default property value if it is null.
18+
19+
### Deprecated
20+
21+
- Nothing.
22+
23+
### Removed
24+
25+
- Nothing.
26+
27+
### Fixed
28+
29+
- [#172](https://github.com/zendframework/zend-code/pull/172) fixes PHP 7.4 compatibility.
30+
531
## 3.3.2 - 2019-08-31
632

733
### Added

src/DeclareStatement.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Zend\Code;
4+
5+
use Zend\Code\Exception\InvalidArgumentException;
6+
7+
class DeclareStatement
8+
{
9+
public const TICKS = 'ticks';
10+
public const STRICT_TYPES = 'strict_types';
11+
public const ENCODING = 'encoding';
12+
13+
private const ALLOWED = [
14+
self::TICKS => 'integer',
15+
self::STRICT_TYPES => 'integer',
16+
self::ENCODING => 'string',
17+
];
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $directive;
23+
24+
/**
25+
* @var int|string
26+
*/
27+
protected $value;
28+
29+
private function __construct(string $directive, $value)
30+
{
31+
$this->directive = $directive;
32+
$this->value = $value;
33+
}
34+
35+
/**
36+
* @return string
37+
*/
38+
public function getDirective(): string
39+
{
40+
return $this->directive;
41+
}
42+
43+
/**
44+
* @return int|string
45+
*/
46+
public function getValue()
47+
{
48+
return $this->value;
49+
}
50+
51+
/**
52+
* @param int $value
53+
* @return self
54+
*/
55+
public static function ticks(int $value): self
56+
{
57+
return new self(self::TICKS, $value);
58+
}
59+
60+
/**
61+
* @param int $value
62+
* @return self
63+
*/
64+
public static function strictTypes(int $value): self
65+
{
66+
return new self(self::STRICT_TYPES, $value);
67+
}
68+
69+
/**
70+
* @param string $value
71+
* @return self
72+
*/
73+
public static function encoding(string $value): self
74+
{
75+
return new self(self::ENCODING, $value);
76+
}
77+
78+
public static function fromArray(array $config): self
79+
{
80+
$directive = key($config);
81+
$value = $config[$directive];
82+
83+
if (! isset(self::ALLOWED[$directive])) {
84+
throw new InvalidArgumentException(
85+
sprintf(
86+
'Declare directive must be one of: %s.',
87+
implode(', ', array_keys(self::ALLOWED))
88+
)
89+
);
90+
}
91+
92+
if (gettype($value) !== self::ALLOWED[$directive]) {
93+
throw new InvalidArgumentException(
94+
sprintf(
95+
'Declare value invalid. Expected %s, got %s.',
96+
self::ALLOWED[$directive],
97+
gettype($value)
98+
)
99+
);
100+
}
101+
102+
$method = str_replace('_', '', lcfirst(ucwords($directive, '_')));
103+
104+
return self::{$method}($value);
105+
}
106+
107+
/**
108+
* @return string
109+
*/
110+
public function getStatement(): string
111+
{
112+
$value = is_string($this->value) ? '\'' . $this->value . '\'' : $this->value;
113+
114+
return sprintf('declare(%s=%s);', $this->directive, $value);
115+
}
116+
}

src/Generator/AbstractMemberGenerator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class AbstractMemberGenerator extends AbstractGenerator
3636
/**#@-*/
3737

3838
/**
39-
* @var DocBlockGenerator
39+
* @var DocBlockGenerator|null
4040
*/
4141
protected $docBlock;
4242

@@ -236,8 +236,13 @@ public function setDocBlock($docBlock)
236236
return $this;
237237
}
238238

239+
public function removeDocBlock(): void
240+
{
241+
$this->docBlock = null;
242+
}
243+
239244
/**
240-
* @return DocBlockGenerator
245+
* @return DocBlockGenerator|null
241246
*/
242247
public function getDocBlock()
243248
{

src/Generator/FileGenerator.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Zend\Code\Generator;
1111

12+
use Zend\Code\DeclareStatement;
13+
use Zend\Code\Exception\InvalidArgumentException;
1214
use Zend\Code\Reflection\Exception as ReflectionException;
1315
use Zend\Code\Reflection\FileReflection;
1416

@@ -72,6 +74,11 @@ class FileGenerator extends AbstractGenerator
7274
*/
7375
protected $body;
7476

77+
/**
78+
* @var DeclareStatement[]
79+
*/
80+
protected $declares = [];
81+
7582
/**
7683
* Passes $options to {@link setOptions()}.
7784
*
@@ -166,6 +173,11 @@ public static function fromArray(array $values)
166173
case 'requiredfiles':
167174
$fileGenerator->setRequiredFiles($value);
168175
break;
176+
case 'declares':
177+
$fileGenerator->setDeclares(array_map(static function ($directive, $value) {
178+
return DeclareStatement::fromArray([$directive => $value]);
179+
}, array_keys($value), $value));
180+
break;
169181
default:
170182
if (property_exists($fileGenerator, $name)) {
171183
$fileGenerator->{$name} = $value;
@@ -408,6 +420,25 @@ public function getBody()
408420
return $this->body;
409421
}
410422

423+
public function setDeclares(array $declares)
424+
{
425+
foreach ($declares as $declare) {
426+
if (! $declare instanceof DeclareStatement) {
427+
throw new InvalidArgumentException(sprintf(
428+
'%s is expecting an array of %s objects',
429+
__METHOD__,
430+
DeclareStatement::class
431+
));
432+
}
433+
434+
if (! array_key_exists($declare->getDirective(), $this->declares)) {
435+
$this->declares[$declare->getDirective()] = $declare;
436+
}
437+
}
438+
439+
return $this;
440+
}
441+
411442
/**
412443
* @return bool
413444
*/
@@ -491,6 +522,28 @@ public function generate()
491522
}
492523
}
493524

525+
// declares, if any
526+
if ($this->declares) {
527+
$declareStatements = '';
528+
529+
foreach ($this->declares as $declare) {
530+
$declareStatements .= $declare->getStatement() . self::LINE_FEED;
531+
}
532+
533+
if (preg_match('#/\* Zend_Code_Generator_FileGenerator-DeclaresMarker \*/#m', $output)) {
534+
$output = preg_replace(
535+
'#/\* Zend_Code_Generator_FileGenerator-DeclaresMarker \*/#m',
536+
$declareStatements,
537+
$output,
538+
1
539+
);
540+
} else {
541+
$output .= $declareStatements;
542+
}
543+
544+
$output .= self::LINE_FEED;
545+
}
546+
494547
// process required files
495548
// @todo marker replacement for required files
496549
$requiredFiles = $this->getRequiredFiles();

src/Generator/PropertyGenerator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public static function fromReflection(PropertyReflection $reflectionProperty)
4646

4747
$allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
4848

49-
$property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]);
49+
$defaultValue = $allDefaultProperties[$reflectionProperty->getName()];
50+
$property->setDefaultValue($defaultValue);
51+
if ($defaultValue === null) {
52+
$property->omitDefaultValue = true;
53+
}
5054

5155
if ($reflectionProperty->getDocComment() != '') {
5256
$property->setDocBlock(DocBlockGenerator::fromReflection($reflectionProperty->getDocBlock()));
@@ -157,7 +161,6 @@ public function __construct($name = null, $defaultValue = null, $flags = self::F
157161
public function setConst($const)
158162
{
159163
if ($const) {
160-
$this->removeFlag(self::FLAG_PUBLIC | self::FLAG_PRIVATE | self::FLAG_PROTECTED);
161164
$this->setFlags(self::FLAG_CONSTANT);
162165
} else {
163166
$this->removeFlag(self::FLAG_CONSTANT);
@@ -227,7 +230,7 @@ public function generate()
227230
$this->name
228231
));
229232
}
230-
$output .= $this->indentation . 'const ' . $name . ' = '
233+
$output .= $this->indentation . $this->getVisibility() . ' const ' . $name . ' = '
231234
. ($defaultValue !== null ? $defaultValue->generate() : 'null;');
232235

233236
return $output;

src/Scanner/ClassScanner.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -912,13 +912,13 @@ protected function scan()
912912
if (is_string($token)) {
913913
$tokenType = null;
914914
$tokenContent = $token;
915-
$tokenLine = $tokenLine + substr_count(
916-
$lastTokenArray[1],
915+
$tokenLine += substr_count(
916+
$lastTokenArray[1] ?? '',
917917
"\n"
918918
); // adjust token line by last known newline count
919919
} else {
920920
$lastTokenArray = $token;
921-
list($tokenType, $tokenContent, $tokenLine) = $token;
921+
[$tokenType, $tokenContent, $tokenLine] = $token;
922922
}
923923

924924
return $tokenIndex;

src/Scanner/MethodScanner.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,13 @@ protected function scan()
451451
if (is_string($token)) {
452452
$tokenType = null;
453453
$tokenContent = $token;
454-
$tokenLine = $tokenLine + substr_count(
455-
$lastTokenArray[1],
454+
$tokenLine += substr_count(
455+
$lastTokenArray[1] ?? '',
456456
"\n"
457457
); // adjust token line by last known newline count
458458
} else {
459-
list($tokenType, $tokenContent, $tokenLine) = $token;
459+
$lastTokenArray = $token;
460+
[$tokenType, $tokenContent, $tokenLine] = $token;
460461
}
461462

462463
return $tokenIndex;

test/Generator/AbstractMemberGeneratorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHPUnit\Framework\TestCase;
1313
use Zend\Code\Generator\AbstractMemberGenerator;
14+
use Zend\Code\Generator\DocBlockGenerator;
1415
use Zend\Code\Generator\Exception\InvalidArgumentException;
1516

1617
class AbstractMemberGeneratorTest extends TestCase
@@ -43,4 +44,21 @@ public function testSetDocBlockThrowsExceptionWithInvalidType()
4344
$this->expectException(InvalidArgumentException::class);
4445
$this->fixture->setDocBlock(new \stdClass());
4546
}
47+
48+
public function testRemoveDocBlock(): void
49+
{
50+
$this->fixture->setDocBlock(new DocBlockGenerator());
51+
52+
$this->fixture->removeDocBlock();
53+
54+
$this->assertNull($this->fixture->getDocBlock());
55+
}
56+
57+
public function testRemoveDocBlockIsIdempotent(): void
58+
{
59+
$this->fixture->removeDocBlock();
60+
$this->fixture->removeDocBlock();
61+
62+
$this->assertNull($this->fixture->getDocBlock());
63+
}
4664
}

test/Generator/ClassGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ public function testClassCanBeGeneratedWithConstantAndPropertyWithSameName()
802802
class TestSampleSingleClass
803803
{
804804
805-
const fooProperty = 'duplicate';
805+
public const fooProperty = 'duplicate';
806806
807807
public $fooProperty = true;
808808

0 commit comments

Comments
 (0)