WsdlToPhp / PackageGenerator

Generates a PHP SDK based on a WSDL, simple and powerful, WSDL to PHP
https://providr.io
MIT License
418 stars 73 forks source link

Type-Error when using regex pattern for decimal restriction #285

Closed tbreuss closed 1 year ago

tbreuss commented 1 year ago

Describe the bug

There is a type error issue when using a regex pattern for a decimal restriction.

In a project we are using the following XSD restriction:

<xs:restriction base="xs:decimal">
    <xs:pattern value="[\-]?[0-9]+\.[0-9]{2}"/>
</xs:restriction>

The generated PHP part looks like this:

<?php

declare(strict_types=1);

...

// validation for constraint: float
if (!is_null($secondOperand) && !(is_float($secondOperand) || is_numeric($secondOperand))) {
    throw new InvalidArgumentException(sprintf('Invalid value %s, please provide a float value, %s given', var_export($secondOperand, true), gettype($secondOperand)), __LINE__);
}
// validation for constraint: pattern([\-]?[0-9]+\.[0-9]{2})
if (!is_null($secondOperand) && !preg_match('/[\\-]?[0-9]+\\.[0-9]{2}/', $secondOperand)) {
    throw new InvalidArgumentException(sprintf('Invalid value %s, please provide a literal that is among the set of character sequences denoted by the regular expression /[\\-]?[0-9]+\\.[0-9]{2}/', var_export($secondOperand, true)), __LINE__);
}

This leads to the following PHP error:

Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, float given in 

The error is caused by the strict type checking in >= PHP 8.1, where the second argument of preg_match is a string, while it is in fact a float.

It could maybe fixed by doing an explicit (string) casting.

To Reproduce The issue occurs during the usage of the generated package I think it is quite clear, what happens. If not, I could provide the wsdl and xsd files.

Expected behavior Well, no type error :-)

tbreuss commented 1 year ago

@mikaelcom I could review #288... if you want.