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

Commit 9de93f9

Browse files
committed
Merge branch 'develop'
Merging develop to master in preparation for 3.0.
2 parents 4972c33 + 7955038 commit 9de93f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1716
-239
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
.gitattributes export-ignore
55
.gitignore export-ignore
66
.travis.yml export-ignore
7-
.php_cs export-ignore
7+
phpcs.xml.dist export-ignore
88
phpunit.xml.dist export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ tmp/
1111
clover.xml
1212
composer.lock
1313
coveralls-upload.json
14+
phpcs.xml
1415
phpunit.xml
1516
vendor

.php_cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ matrix:
2727
- php: 7
2828
- php: hhvm
2929
allow_failures:
30-
- php: 7
3130
- php: hhvm
3231

3332
notifications:
@@ -45,7 +44,7 @@ install:
4544
script:
4645
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/phpunit --coverage-clover clover.xml ; fi
4746
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then ./vendor/bin/phpunit ; fi
48-
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/php-cs-fixer fix -v --diff --dry-run ; fi
47+
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/phpcs ; fi
4948

5049
after_script:
5150
- if [[ $EXECUTE_TEST_COVERALLS == 'true' ]]; then ./vendor/bin/coveralls ; fi

CHANGELOG.md

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

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

5-
## 2.6.3 - TBD
5+
## 3.0.0 - TBD
6+
7+
### Changed
8+
9+
This section refers to breaking changes: please refer to
10+
[doc/book/zend.code.migrate.2.to.3.md](doc/book/zend.code.migrate.2.to.3.md) for migration instructions.
11+
12+
- Types `string`, `int`, `float`, `bool` passed to `Zend\Code\Generator\ParameterGenerator#setType()`
13+
are no longer ignored in generated code [#30](https://github.com/zendframework/zend-code/pull/30)
14+
- Types declared in DocBlocks are now ignored when creating a `Zend\Code\Generator\ParameterGenerator` via
15+
`Zend\Code\Generator\ParameterGenerator::fromReflection()`. [#30](https://github.com/zendframework/zend-code/pull/30)
16+
- Type strings are now validated: passing an invalid type to any method in the generator API
17+
may lead to a `Zend\Code\Generator\InvalidArgumentException` being thrown.
18+
[#30](https://github.com/zendframework/zend-code/pull/30)
19+
- `Zend\Code\Generator\ParameterGenerator::$simple` was removed. [#30](https://github.com/zendframework/zend-code/pull/30)
20+
- `Zend\Code\Generator\ParameterGenerator#$type` is now a `null|Zend\Code\Generator\TypeGenerator`: was a
21+
`string` before. [#30](https://github.com/zendframework/zend-code/pull/30)
22+
- `Zend\Code\Generator` type-hints are now always prefixed with the namespace separator `\`.
23+
[#30](https://github.com/zendframework/zend-code/pull/30)
24+
- `Zend\Code\Reflection\ParameterReflection#getType()` was renamed
25+
to `Zend\Code\Reflection\ParameterReflection#detectType()` in order to not override the inherited
26+
`ReflectionParameter#getType()`, introduced in PHP 7. [#30](https://github.com/zendframework/zend-code/pull/30)
27+
28+
### Added
29+
30+
- PHP 7 return type hints generation support via `Zend\Code\Generator\MethodGenerator#setReturnType()`.
31+
[#30](https://github.com/zendframework/zend-code/pull/30)
32+
- PHP 7 scalar type hints generation support via `Zend\Code\Generator\ParameterGenerator#setType()` and
33+
`Zend\Code\Generator\ParameterGenerator#getType()`. [#30](https://github.com/zendframework/zend-code/pull/30)
34+
- PHP 5.6 variadic arguments support via `Zend\Code\Generator\ParameterGenerator#setVariadic()` and
35+
`Zend\Code\Generator\ParameterGenerator#getVariadic()`. [#30](https://github.com/zendframework/zend-code/pull/30)
36+
- Generation of methods returning by reference is supported via `Zend\Code\Generator\ParameterGenerator#setReturnsReference()`.
37+
[#30](https://github.com/zendframework/zend-code/pull/30)
38+
39+
### Deprecated
40+
41+
- Nothing.
42+
43+
### Removed
44+
45+
- `Zend\Code\ParameterGenerator::$simple` was removed. [#30](https://github.com/zendframework/zend-code/pull/30)
46+
47+
### Fixed
48+
49+
- Nothing.
50+
51+
## 2.6.2 - 2015-01-05
652

753
### Added
854

@@ -18,7 +64,7 @@ All notable changes to this project will be documented in this file, in reverse
1864

1965
### Fixed
2066

21-
- Nothing.
67+
- [#31](https://github.com/zendframework/zend-code/pull/31) updated license year.
2268

2369
## 2.6.2 - 2015-01-05
2470

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
}
1414
},
1515
"require": {
16-
"php": ">=5.5",
17-
"zendframework/zend-eventmanager": "^2.6|^3.0"
16+
"php": "^5.5 || ^7.0",
17+
"zendframework/zend-eventmanager": "^2.6|^3.0",
18+
"squizlabs/php_codesniffer": "^2.5"
1819
},
1920
"require-dev": {
21+
"ext-phar": "*",
2022
"doctrine/annotations": "~1.0",
2123
"zendframework/zend-stdlib": "~2.7",
22-
"fabpot/php-cs-fixer": "1.7.*",
23-
"phpunit/PHPUnit": "~4.0"
24+
"phpunit/PHPUnit": "^4.8.21"
2425
},
2526
"suggest": {
2627
"doctrine/annotations": "Doctrine\\Common\\Annotations >=1.0 for annotation features",
@@ -31,7 +32,7 @@
3132
"extra": {
3233
"branch-alias": {
3334
"dev-master": "2.6-dev",
34-
"dev-develop": "2.7-dev"
35+
"dev-develop": "3.0-dev"
3536
}
3637
},
3738
"autoload-dev": {
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Migrating from `Zend\Code` `2.*` to `3.0.0`
2+
3+
### `string`, `int`, `float`, `bool` are no longer ignored
4+
5+
In 2.x, a `Zend\Code\Generator\ParameterGenerator` with name `foo` and type
6+
`string`, `int`, `float` or `bool` simply generated code `"$foo"`:
7+
8+
```php
9+
$generator = new \Zend\Code\ParameterGenerator('foo');
10+
11+
$generator->setType('string');
12+
13+
echo $generator->generate(); // "$foo"
14+
```
15+
16+
In 3.x, this code will instead produce `"string $foo"`.
17+
If you generate code that should run in PHP 5.x, it is advisable to strip
18+
`string`, `int`, `float` and `bool` from type definitions passed to
19+
`Zend\Code\ParameterGenerator` instances. The quickest way is to set the
20+
type to `null`, if it matches any of these scalar types:
21+
22+
```php
23+
if (in_array($type, ['string', 'int', 'float', 'bool'])) {
24+
$type = null;
25+
}
26+
27+
$generator->setType($type);
28+
```
29+
30+
### `Zend\Code\Reflection\ParameterReflection#getType()` changes
31+
32+
PHP 7 introduced [`ReflectionParameter#getType()`](http://php.net/manual/en/reflectionparameter.gettype.php).
33+
34+
In order to not override this method, `Zend\Code\Reflection\ParameterReflection#getType()`
35+
was renamed to `Zend\Code\Reflection\ParameterReflection#detectType()`.
36+
37+
If you relied on `Zend\Code\Reflection\ParameterReflection#getType()`, you can
38+
simply replace the method calls in your code.
39+
40+
### DocBlock types ignored by `Zend\Code\Generator\ParameterGenerator::fromReflection()`
41+
42+
As a direct consequence of the previous change, calls to
43+
`Zend\Code\Generator\ParameterGenerator::fromReflection()` will not mirror the
44+
type hints read from a method's DocBlock.
45+
46+
As an example, take following code:
47+
48+
```php
49+
class Foo
50+
{
51+
/**
52+
* @param string $baz
53+
*/
54+
public function bar($baz)
55+
{
56+
}
57+
}
58+
59+
$methodGenerator = \Zend\Code\Generator\MethodGenerator::fromReflection(
60+
new \Zend\Code\Reflection\MethodReflection('Foo', 'bar')
61+
);
62+
63+
var_dump($methodGenerator->getParameters()[0]->getType());
64+
```
65+
66+
In version 2.x, this code produces `"string"`, in version 3.x it returns `null`. If you
67+
need to rely on the types in the annotations, please use
68+
`Zend\Code\Reflection\ParameterReflection#detectType()` instead, and build a
69+
`MethodGenerator` instance manually.
70+
71+
This change is required: since signatures in PHP 7 include scalar type hints.
72+
That also means that reflecting scalar type hints from DocBlocks into the
73+
signature of a generated method may lead to fatal errors (due to signature
74+
mismatch) at runtime.
75+
76+
### Type strings are validated
77+
78+
If you attempt to generate type-hints for parameters or return types, those types are
79+
now validated before the code is generated.
80+
81+
Be sure to check which values you pass to `Zend\Code\Generator\MethodGenerator#setReturnType()`
82+
or `Zend\Code\Generator\ParameterGenerator#setType()`, as you may incur in a
83+
`Zend\Code\Generator\Exception\InvalidArgumentException` being thrown if any
84+
of those types are invalid strings:
85+
86+
```php
87+
$parameterGenerator->setType('foo'); // valid
88+
$parameterGenerator->setType('array'); // valid
89+
$parameterGenerator->setType('bool'); // valid
90+
$parameterGenerator->setType('123'); // invalid (throws exception)
91+
$parameterGenerator->setType(''); // invalid (throws exception)
92+
$parameterGenerator->setType('*'); // invalid (throws exception)
93+
$parameterGenerator->setType('\\'); // invalid (throws exception)
94+
```
95+
96+
97+
### Generated type-hints are now prefixed by `"\"`
98+
99+
Generated type-hints are now prefixed with the `NAMESPACE_SEPARATOR`,
100+
`"\"`.
101+
102+
Take following example code:
103+
104+
```php
105+
$parameter = new \Zend\Code\Generator\ParameterGenerator('bar', 'baz');
106+
$method = new \Zend\Code\Generator\MethodGenerator('foo', [$parameter]);
107+
108+
$method->setReturnType('tab');
109+
110+
echo $method->generate();
111+
```
112+
113+
This code produces `public function foo(baz $bar) {}` in 2.x.
114+
In version 3.x, it produces `public function foo(\baz $bar) : \tab {}`.
115+
116+
In order to avoid migration problems, be sure to always pass fully qualified class
117+
names to `Zend\Code\Generator\MethodGenerator` and `Zend\Code\Generator\ParameterGenerator`.
118+
119+
120+
### `Zend\Code\Generator\ParameterGenerator::$simple` was removed
121+
122+
If you extended `Zend\Code\Generator\ParameterGenerator`, be sure to check if you
123+
are accessing the protected static variable `$simple`: it was removed, and you should
124+
adapt your code by either copying it into your class or avoiding its usage.
125+
126+
### `Zend\Code\Generator\ParameterGenerator::$type` has changed
127+
128+
If you extended `Zend\Code\Generator\ParameterGenerator`, be sure to check if you
129+
are accessing the protected variable `$type`: its type has changed.
130+
While it can still be used as a string via an explicit `(string)` cast, the type of
131+
this protected member is now `null|Zend\Code\Generator\TypeGenerator`.

doc/bookdown.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"content": [
55
"book/zend.code.generator.introduction.md",
66
"book/zend.code.generator.reference.md",
7-
"book/zend.code.generator.examples.md"
7+
"book/zend.code.generator.examples.md",
8+
"book/zend.code.migrate.2.to.3.md"
89
]
910
}

phpcs.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Zend Framework coding standard">
3+
<description>Zend Framework coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<arg name="colors"/>
8+
9+
<!-- inherit rules from: -->
10+
<rule ref="PSR2"/>
11+
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
12+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
13+
<properties>
14+
<property name="ignoreBlankLines" value="false"/>
15+
</properties>
16+
</rule>
17+
18+
<!-- Paths to check -->
19+
<file>src</file>
20+
<file>test</file>
21+
<exclude-pattern>*/TestAsset/*</exclude-pattern>
22+
<exclude-pattern>*/_files/*</exclude-pattern>
23+
</ruleset>

src/Generator/FileGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ public static function fromArray(array $values)
134134
$fileGenerator->setFilename($value);
135135
continue;
136136
case 'class':
137-
$fileGenerator->setClass(($value instanceof ClassGenerator) ? $value : ClassGenerator::fromArray($value));
137+
$fileGenerator->setClass(
138+
($value instanceof ClassGenerator)
139+
? $value
140+
: ClassGenerator::fromArray($value)
141+
);
138142
continue;
139143
case 'requiredfiles':
140144
$fileGenerator->setRequiredFiles($value);
@@ -438,7 +442,9 @@ public function generate()
438442
$docBlock->setIndentation('');
439443

440444
if (preg_match('#/\* Zend_Code_Generator_FileGenerator-DocBlockMarker \*/#m', $output)) {
445+
// @codingStandardsIgnoreStart
441446
$output = preg_replace('#/\* Zend_Code_Generator_FileGenerator-DocBlockMarker \*/#m', $docBlock->generate(), $output, 1);
447+
// @codingStandardsIgnoreEnd
442448
} else {
443449
$output .= $docBlock->generate() . self::LINE_FEED;
444450
}
@@ -521,7 +527,9 @@ public function generate()
521527
// process classes
522528
if (!empty($classes)) {
523529
foreach ($classes as $class) {
530+
// @codingStandardsIgnoreStart
524531
$regex = str_replace('&', $class->getName(), '/\* Zend_Code_Generator_Php_File-ClassMarker: \{[A-Za-z0-9\\\]+?&\} \*/');
532+
// @codingStandardsIgnoreEnd
525533
if (preg_match('#' . $regex . '#m', $output)) {
526534
$output = preg_replace('#' . $regex . '#', $class->generate(), $output, 1);
527535
} else {

0 commit comments

Comments
 (0)