|
| 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`. |
0 commit comments