-
Notifications
You must be signed in to change notification settings - Fork 20
Writing defaults of class properties #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writing defaults of class properties #29
Conversation
There should not be any defaults in the setter methods of class properties. These should only be assigned in the initialization process of an object (constructor).
src/JsonSchema/PhpBuilder.php
Outdated
@@ -176,6 +176,10 @@ private function makeClass($schema, $path) | |||
$phpProperty->addMeta($property, self::SCHEMA); | |||
$phpProperty->addMeta($name, self::PROPERTY_NAME); | |||
|
|||
if (!is_null($property->default)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make this configurable, rendering default can be unwanted in some cases, e.g. to avoid default values when encoding json.
We can have some option like this in PhpBuilder
:
/**
* Use default values to initialize properties
* @var bool
*/
public $declarePropertyDefaults = false;
and then
if (!is_null($property->default) && $this->declarePropertyDefaults) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point
Codecov Report
@@ Coverage Diff @@
## master #29 +/- ##
============================================
+ Coverage 88.23% 89.02% +0.78%
- Complexity 500 503 +3
============================================
Files 38 38
Lines 1326 1330 +4
============================================
+ Hits 1170 1184 +14
+ Misses 156 146 -10
Continue to review full report at Codecov.
|
@derjumpy thank you, |
If you work with the rendered classes to export data, there are no default values for the class properties. With this pull request the default values will be written to the generated files. The setter methods will not get any defaults, because this should only be done in the definition of the class properties.
Class properties (first without a default value):
Setter would be the same as before: