Skip to content

Commit

Permalink
Add link to the error message to a property when it is required. (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: david_smith <[email protected]>
  • Loading branch information
zero-to-prod and david_smith authored Oct 25, 2024
1 parent 4ea1efc commit ab16d82
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/DataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,23 @@ public static function from(iterable|object|null $context = []): self
continue;
}
if (isset($Describe->required) && $Describe->required) {
throw new PropertyRequiredException("Property: $property_name is required");
$lineNumber = static function (string $filename, string $property_name): ?int {
foreach (file($filename) as $line_number => $content) {
if (preg_match("/\\$$property_name/", $content)) {
return $line_number + 1;
}
}

return null;
};
throw new PropertyRequiredException(
sprintf(
"Property `$%s` is required.\n%s:%d",
$property_name,
$ReflectionClass->getFileName(),
$lineNumber($ReflectionClass->getFileName(), $property_name),
)
);
}
if (isset($Describe->missing_as_null) && $Describe?->missing_as_null) {
$self->{$property_name} = null;
Expand Down

0 comments on commit ab16d82

Please sign in to comment.