Skip to content

Commit d73d2f8

Browse files
authored
issue #285 - Cast value passed to preg_match to a string (#288)
* issue #285 - Cast value passed to preg_match to a string * issue #285 - Avoid pattern rule generation for boolean
1 parent cda1c32 commit d73d2f8

18 files changed

+256
-24
lines changed

src/File/Validation/AbstractRule.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ final public function applyRule(string $parameterName, $value, bool $itemType =
4242

4343
final public function validationRuleComment($value): string
4444
{
45-
return sprintf('// %s %s%s', self::VALIDATION_RULE_COMMENT_SENTENCE, $this->name(), is_array($value) ? sprintf('(%s)', implode(', ', array_unique($value))) : (empty($value) ? '' : sprintf('(%s)', $value)));
45+
return sprintf(
46+
'// %s %s%s',
47+
self::VALIDATION_RULE_COMMENT_SENTENCE,
48+
$this->name(),
49+
is_array($value) ? sprintf('(%s)', implode(', ', array_unique($value))) : (empty($value) ? '' : sprintf('(%s)', $value))
50+
);
4651
}
4752

4853
abstract public function name(): string;

src/File/Validation/PatternRule.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ public function name(): string
2222
public function testConditions(string $parameterName, $value, bool $itemType = false): string
2323
{
2424
if ($itemType || !$this->getAttribute()->isArray()) {
25+
$valueToMatch = self::valueToRegularExpression($value);
26+
if (empty($valueToMatch)) {
27+
return '';
28+
}
29+
2530
$test = sprintf(
26-
($itemType ? '' : '!is_null($%1$s) && ').'!preg_match(\'/%2$s/\', $%1$s)',
31+
($itemType ? '' : '!is_null($%1$s) && ').'!preg_match(\'/%2$s/\', (string) $%1$s)',
2732
$parameterName,
2833
self::valueToRegularExpression($value)
2934
);
@@ -63,7 +68,13 @@ public static function valueToRegularExpression($value): string
6368
static fn ($value) => addcslashes($value, '\'\\/'),
6469
array_map(
6570
static fn ($value) => empty($value) ? '^$' : $value,
66-
array_map('trim', is_array($value) ? $value : [$value])
71+
array_map(
72+
'trim',
73+
array_filter(
74+
is_array($value) ? $value : [$value],
75+
static fn ($value) => !in_array($value, ['true', 'false', true, false], true)
76+
)
77+
)
6778
)
6879
)
6980
);

src/File/Validation/UnionRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ protected function addValidationMethod(string $parameterName, array $unionValues
5959
unset($attribute);
6060
}
6161

62-
// adapt content, remove duplicated rules
63-
// duplicated rules is base don the fact that validation rules are composed by 4 lines so we check existing rule every 4-line block of text
62+
// Adapt content, remove duplicated rules
63+
// The duplicated rules are based on the fact that validation rules are composed by 4 lines so we check existing rule every 4-line block of text
6464
$exceptions = 0;
6565
$exceptionsTests = [];
6666
$exceptionsArray = [];
@@ -69,7 +69,7 @@ protected function addValidationMethod(string $parameterName, array $unionValues
6969
$childrenCount = count($methodChildren);
7070
$existingValidationRules = [];
7171
for ($i = 0; $i < $childrenCount; $i += 4) {
72-
$validationRules = array_slice($methodChildren, ((int) $i / 4) * 4, 4);
72+
$validationRules = array_slice($methodChildren, ((int) ($i / 4)) * 4, 4);
7373
if (!in_array($validationRules, $existingValidationRules)) {
7474
foreach ($validationRules as $validationRuleIndex => $validationRule) {
7575
// avoid having a validation rule that has already been applied to the attribute within the method which is calling the validate method

tests/File/StructTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ public function testWriteBingSearchStructSearchRequest(): void
115115
}
116116
}
117117

118+
public function testWriteBingSearchControlsType(): void
119+
{
120+
$generator = self::bingGeneratorInstance();
121+
if (($model = $generator->getStructByName('ControlsType')) instanceof StructModel) {
122+
$struct = new StructFile($generator, $model->getName());
123+
$struct
124+
->setModel($model)
125+
->write()
126+
;
127+
$this->assertSameFileContent('ValidApiControlsType', $struct);
128+
} else {
129+
$this->fail('Unable to find ControlsType struct for file generation');
130+
}
131+
}
132+
118133
public function testWriteActonStructItem(): void
119134
{
120135
$generator = self::actonGeneratorInstance();

tests/resources/bingsearch.wsdl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@
9696
<xsd:element minOccurs="0" maxOccurs="unbounded" name="string" type="tns:guid" />
9797
</xsd:sequence>
9898
</xsd:complexType>
99+
<xsd:simpleType name="SimpleBooleanType">
100+
<xsd:restriction base="xsd:boolean">
101+
<xsd:pattern value="true"/>
102+
<xsd:pattern value="false"/>
103+
</xsd:restriction>
104+
</xsd:simpleType>
105+
<xsd:complexType name="ControlsType">
106+
<xsd:sequence>
107+
<xsd:element name="ProcessByDistributor" type="tns:SimpleBooleanType">
108+
</xsd:element>
109+
</xsd:sequence>
110+
</xsd:complexType>
99111
<xsd:complexType name="ArrayOfString">
100112
<xsd:sequence>
101113
<xsd:element minOccurs="0" maxOccurs="unbounded" name="string" type="xsd:string" />

tests/resources/generated/ValidAddressType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public function setType(?string $type = null): self
479479
throw new InvalidArgumentException(sprintf('Invalid value %s, please provide a string, %s given', var_export($type, true), gettype($type)), __LINE__);
480480
}
481481
// validation for constraint: pattern([0-9A-Z]{1,3}(\.[A-Z]{3}(\.X){0,1}){0,1}, 0AA.BBBX, )
482-
if (!is_null($type) && !preg_match('/[0-9A-Z]{1,3}(\\.[A-Z]{3}(\\.X){0,1}){0,1}|0AA.BBBX|^$/', $type)) {
482+
if (!is_null($type) && !preg_match('/[0-9A-Z]{1,3}(\\.[A-Z]{3}(\\.X){0,1}){0,1}|0AA.BBBX|^$/', (string) $type)) {
483483
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-9A-Z]{1,3}(\\.[A-Z]{3}(\\.X){0,1}){0,1}|0AA.BBBX|^$/', var_export($type, true)), __LINE__);
484484
}
485485
$this->Type = $type;

tests/resources/generated/ValidApiArrayOfGuid.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function validateStringForPatternConstraintFromSetString(?array $v
8383
$invalidValues = [];
8484
foreach (($values ?? []) as $arrayOfGuidStringItem) {
8585
// validation for constraint: pattern([\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12})
86-
if (!preg_match('/[\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}/', $arrayOfGuidStringItem)) {
86+
if (!preg_match('/[\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}/', (string) $arrayOfGuidStringItem)) {
8787
$invalidValues[] = var_export($arrayOfGuidStringItem, true);
8888
}
8989
}
@@ -175,7 +175,7 @@ public function add($item): self
175175
throw new InvalidArgumentException(sprintf('The string property can only contain items of type string, %s given', is_object($item) ? get_class($item) : (is_array($item) ? implode(', ', $item) : gettype($item))), __LINE__);
176176
}
177177
// validation for constraint: pattern([\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12})
178-
if (!preg_match('/[\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}/', $item)) {
178+
if (!preg_match('/[\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}/', (string) $item)) {
179179
throw new InvalidArgumentException(sprintf('Invalid value %s, please provide a literal that is among the set of character sequences denoted by the regular expression /[\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}/', var_export($item, true)), __LINE__);
180180
}
181181
return parent::add($item);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace StructType;
6+
7+
use InvalidArgumentException;
8+
use WsdlToPhp\PackageBase\AbstractStructBase;
9+
10+
/**
11+
* This class stands for ControlsType StructType
12+
* @package Api
13+
* @subpackage Structs
14+
* @release 1.1.0
15+
*/
16+
class ApiControlsType extends AbstractStructBase
17+
{
18+
/**
19+
* The ProcessByDistributor
20+
* Meta information extracted from the WSDL
21+
* - base: xsd:boolean
22+
* - pattern: true | false
23+
* @var bool|null
24+
*/
25+
protected ?bool $ProcessByDistributor = null;
26+
/**
27+
* Constructor method for ControlsType
28+
* @uses ApiControlsType::setProcessByDistributor()
29+
* @param bool $processByDistributor
30+
*/
31+
public function __construct(?bool $processByDistributor = null)
32+
{
33+
$this
34+
->setProcessByDistributor($processByDistributor);
35+
}
36+
/**
37+
* Get ProcessByDistributor value
38+
* @return bool|null
39+
*/
40+
public function getProcessByDistributor(): ?bool
41+
{
42+
return $this->ProcessByDistributor;
43+
}
44+
/**
45+
* Set ProcessByDistributor value
46+
* @param bool $processByDistributor
47+
* @return \StructType\ApiControlsType
48+
*/
49+
public function setProcessByDistributor(?bool $processByDistributor = null): self
50+
{
51+
// validation for constraint: boolean
52+
if (!is_null($processByDistributor) && !is_bool($processByDistributor)) {
53+
throw new InvalidArgumentException(sprintf('Invalid value %s, please provide a bool, %s given', var_export($processByDistributor, true), gettype($processByDistributor)), __LINE__);
54+
}
55+
$this->ProcessByDistributor = $processByDistributor;
56+
57+
return $this;
58+
}
59+
}

tests/resources/generated/ValidBingClassMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ final public static function get(): array
2121
'WebRequest' => '\\StructType\\ApiWebRequest',
2222
'ArrayOfWebSearchOption' => '\\ArrayType\\ApiArrayOfWebSearchOption',
2323
'ArrayOfGuid' => '\\ArrayType\\ApiArrayOfGuid',
24+
'ControlsType' => '\\StructType\\ApiControlsType',
2425
'ArrayOfString' => '\\ArrayType\\ApiArrayOfString',
2526
'ImageRequest' => '\\StructType\\ApiImageRequest',
2627
'PhonebookRequest' => '\\StructType\\ApiPhonebookRequest',

tests/resources/generated/ValidExpiryDate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function setMonth(string $month): self
6666
throw new InvalidArgumentException(sprintf('Invalid value %s, please provide a string, %s given', var_export($month, true), gettype($month)), __LINE__);
6767
}
6868
// validation for constraint: pattern((0[1-9]|1[012]))
69-
if (!is_null($month) && !preg_match('/(0[1-9]|1[012])/', $month)) {
69+
if (!is_null($month) && !preg_match('/(0[1-9]|1[012])/', (string) $month)) {
7070
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[1-9]|1[012])/', var_export($month, true)), __LINE__);
7171
}
7272
$this->month = $month;
@@ -93,7 +93,7 @@ public function setYear(string $year): self
9393
throw new InvalidArgumentException(sprintf('Invalid value %s, please provide a string, %s given', var_export($year, true), gettype($year)), __LINE__);
9494
}
9595
// validation for constraint: pattern([0-9][0-9])
96-
if (!is_null($year) && !preg_match('/[0-9][0-9]/', $year)) {
96+
if (!is_null($year) && !preg_match('/[0-9][0-9]/', (string) $year)) {
9797
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]/', var_export($year, true)), __LINE__);
9898
}
9999
$this->year = $year;

0 commit comments

Comments
 (0)