swaggest / php-json-schema

High definition PHP structures with JSON-schema based validation
MIT License
451 stars 51 forks source link

Support typed properties #147

Closed bruceoutdoors closed 2 years ago

bruceoutdoors commented 2 years ago

PHP doesn't allow typed properties to be accessed if it is uninitialized:

PHP Fatal error:  Uncaught Error: Typed property Order::$user_id must not be accessed before initialization

This change skips properties if it is not set. I know this library doesn't really support PHP type declarations, but this is a pretty harmless change that does not affect existing functionality (:

codecov[bot] commented 2 years ago

Codecov Report

Merging #147 (6ad14f1) into master (0ee6870) will decrease coverage by 0.22%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #147      +/-   ##
==========================================
- Coverage   92.64%   92.41%   -0.23%     
==========================================
  Files          27       27              
  Lines        1686     1688       +2     
==========================================
- Hits         1562     1560       -2     
- Misses        124      128       +4     
Impacted Files Coverage Δ
src/Structure/ClassStructureTrait.php 90.54% <100.00%> (-5.30%) :arrow_down:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

vearutop commented 2 years ago

Thank you! 💪

bruceoutdoors commented 2 years ago

Oh oo. I think those are valid points from the CI. Let me take a look

vearutop commented 2 years ago

Hehe, no worries, I'll also look at it a bit later (and will fix red cross for cloc for external contributors).

bruceoutdoors commented 2 years ago

Ah this change will break nullable fields. The correct implementation should be

// Get uninitialized properties as null
$value = isset($this->$propertyName) ? $this->$propertyName : null;

I'll send another PR. Apologies.