Skip to content

Commit a31d48e

Browse files
authored
issue #274 - Fix validation on scalar contained by array
Improve annotation block generation for constraint validation methods
1 parent 9c7e6ca commit a31d48e

File tree

109 files changed

+1164
-744
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+1164
-744
lines changed

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
->setUsingCache(false)
1010
->setRules(array(
1111
'@PhpCsFixer' => true,
12+
'phpdoc_separation' => false,
1213
))
1314
->setFinder($finder);

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@
107107
"wsdltophp/wsdlhandler": "^1.0"
108108
},
109109
"scripts": {
110-
"test": "vendor/bin/phpunit",
111-
"lint": "vendor/bin/php-cs-fixer fix --ansi --diff --verbose",
112-
"build": "box build --verbose",
113-
"phpstan": "vendor/bin/phpstan analyze src --level=2"
110+
"test": "php-7.4 vendor/bin/phpunit",
111+
"lint": "php-7.4 vendor/bin/php-cs-fixer fix --ansi --diff --verbose",
112+
"build": "php-7.4 box build --verbose",
113+
"phpstan": "php-7.4 vendor/bin/phpstan analyze src --level=2"
114114
},
115115
"require-dev": {
116116
"friendsofphp/php-cs-fixer": "^3.0",

src/Command/GeneratePackageCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace WsdlToPhp\PackageGenerator\Command;
66

7-
use DateTime;
87
use Symfony\Component\Console\Input\InputInterface;
98
use Symfony\Component\Console\Input\InputOption;
109
use Symfony\Component\Console\Output\OutputInterface;
@@ -276,7 +275,7 @@ protected function configure(): void
276275
protected function execute(InputInterface $input, OutputInterface $output): int
277276
{
278277
parent::execute($input, $output);
279-
$start = new DateTime();
278+
$start = new \DateTime();
280279
$this->writeLn(sprintf(' Start at %s', $start->format('Y-m-d H:i:s')));
281280
$this->initGeneratorOptions();
282281
if ($this->canExecute()) {
@@ -287,7 +286,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
287286
$this->writeLn(" Used generator's options:");
288287
$this->writeLn(' '.implode(PHP_EOL.' ', $this->formatArrayForConsole($this->generatorOptions->toArray())));
289288
}
290-
$end = new DateTime();
289+
$end = new \DateTime();
291290
$this->writeLn(sprintf(' End at %s, duration: %s', $end->format('Y-m-d H:i:s'), $start->diff($end)->format('%H:%I:%S')));
292291

293292
return self::EXIT_OK;

src/ConfigurationReader/AbstractYamlReader.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace WsdlToPhp\PackageGenerator\ConfigurationReader;
66

7-
use InvalidArgumentException;
87
use Symfony\Component\Yaml\Parser;
98

109
abstract class AbstractYamlReader
@@ -21,7 +20,7 @@ public static function instance(?string $filename = null): self
2120
{
2221
$loadFilename = empty($filename) ? static::getDefaultConfigurationPath() : $filename;
2322
if (empty($loadFilename) || !is_file($loadFilename)) {
24-
throw new InvalidArgumentException(sprintf('Unable to locate file "%s"', $loadFilename), __LINE__);
23+
throw new \InvalidArgumentException(sprintf('Unable to locate file "%s"', $loadFilename), __LINE__);
2524
}
2625

2726
$key = sprintf('%s_%s', get_called_class(), $loadFilename);
@@ -51,7 +50,7 @@ protected function parseSimpleArray(string $filename, string $mainKey): array
5150
{
5251
$values = $this->loadYaml($filename);
5352
if (!array_key_exists($mainKey, $values)) {
54-
throw new InvalidArgumentException(sprintf('Unable to find section "%s" in "%s"', $mainKey, $filename), __LINE__);
53+
throw new \InvalidArgumentException(sprintf('Unable to find section "%s" in "%s"', $mainKey, $filename), __LINE__);
5554
}
5655

5756
return $values[$mainKey];

src/ConfigurationReader/GeneratorOptions.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace WsdlToPhp\PackageGenerator\ConfigurationReader;
66

7-
use InvalidArgumentException;
8-
use JsonSerializable;
97
use WsdlToPhp\PackageGenerator\Model\StructValue;
108

119
/**
@@ -75,7 +73,7 @@
7573
* @method string getXsdTypesPath()
7674
* @method self setXsdTypesPath(string $xsdTypesPath)
7775
*/
78-
final class GeneratorOptions extends AbstractYamlReader implements JsonSerializable
76+
final class GeneratorOptions extends AbstractYamlReader implements \JsonSerializable
7977
{
8078
/**
8179
* Common values used as option's value.
@@ -153,7 +151,7 @@ public static function instance(?string $filename = null): self
153151
public function getOptionValue(string $optionName)
154152
{
155153
if (!array_key_exists($optionName, $this->options)) {
156-
throw new InvalidArgumentException(sprintf('Invalid option name "%s", possible options: %s', $optionName, implode(', ', array_keys($this->options))), __LINE__);
154+
throw new \InvalidArgumentException(sprintf('Invalid option name "%s", possible options: %s', $optionName, implode(', ', array_keys($this->options))), __LINE__);
157155
}
158156

159157
return array_key_exists('value', $this->options[$optionName]) ? $this->options[$optionName]['value'] : $this->options[$optionName]['default'];
@@ -167,7 +165,7 @@ public function setOptionValue(string $optionName, $optionValue, array $values =
167165
'values' => $values,
168166
];
169167
} elseif (!empty($this->options[$optionName]['values']) && !in_array($optionValue, $this->options[$optionName]['values'], true)) {
170-
throw new InvalidArgumentException(sprintf('Invalid value "%s" for option "%s", possible values: %s', $optionValue, $optionName, implode(', ', $this->options[$optionName]['values'])), __LINE__);
168+
throw new \InvalidArgumentException(sprintf('Invalid value "%s" for option "%s", possible values: %s', $optionValue, $optionName, implode(', ', $this->options[$optionName]['values'])), __LINE__);
171169
} else {
172170
$this->options[$optionName]['value'] = $optionValue;
173171
}
@@ -253,7 +251,7 @@ protected function parseOptions(string $filename): self
253251
if (is_array($options)) {
254252
$this->options = $options;
255253
} else {
256-
throw new InvalidArgumentException(sprintf('Settings contained by "%s" are not valid as the settings are not contained by an array: "%s"', $filename, gettype($options)), __LINE__);
254+
throw new \InvalidArgumentException(sprintf('Settings contained by "%s" are not valid as the settings are not contained by an array: "%s"', $filename, gettype($options)), __LINE__);
257255
}
258256

259257
return $this;

src/Container/AbstractObjectContainer.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44

55
namespace WsdlToPhp\PackageGenerator\Container;
66

7-
use ArrayAccess;
8-
use Countable;
9-
use InvalidArgumentException;
10-
use Iterator;
11-
use JsonSerializable;
127
use WsdlToPhp\PackageGenerator\Generator\AbstractGeneratorAware;
138

14-
abstract class AbstractObjectContainer extends AbstractGeneratorAware implements ArrayAccess, Iterator, Countable, JsonSerializable
9+
abstract class AbstractObjectContainer extends AbstractGeneratorAware implements \ArrayAccess, \Iterator, \Countable, \JsonSerializable
1510
{
1611
public const PROPERTY_NAME = 'name';
1712

@@ -48,7 +43,7 @@ public function offsetGet($offset)
4843
#[\ReturnTypeWillChange]
4944
public function offsetSet($offset, $value)
5045
{
51-
throw new InvalidArgumentException('This method can\'t be used as object are stored with a string as array index', __LINE__);
46+
throw new \InvalidArgumentException('This method can\'t be used as object are stored with a string as array index', __LINE__);
5247
}
5348

5449
#[\ReturnTypeWillChange]
@@ -110,7 +105,7 @@ public function add(object $object): self
110105
public function get($value)
111106
{
112107
if (!is_scalar($value)) {
113-
throw new InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', is_object($value) ? get_class($value) : var_export($value, true), get_class($this)), __LINE__);
108+
throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', is_object($value) ? get_class($value) : var_export($value, true), get_class($this)), __LINE__);
114109
}
115110

116111
return array_key_exists($value, $this->objects) ? $this->objects[$value] : null;
@@ -134,27 +129,27 @@ abstract protected function objectProperty(): string;
134129
/**
135130
* This method is called before the object has been stored.
136131
*
137-
* @throws InvalidArgumentException
132+
* @throws \InvalidArgumentException
138133
*/
139134
protected function beforeObjectIsStored(object $object): void
140135
{
141136
$objectClass = $this->objectClass();
142137

143138
if (!$object instanceof $objectClass) {
144-
throw new InvalidArgumentException(sprintf('Model of type "%s" does not match the object contained by this class: "%s"', get_class($object), $objectClass), __LINE__);
139+
throw new \InvalidArgumentException(sprintf('Model of type "%s" does not match the object contained by this class: "%s"', get_class($object), $objectClass), __LINE__);
145140
}
146141
}
147142

148143
protected function getObjectKey(object $object)
149144
{
150145
$get = sprintf('get%s', ucfirst($this->objectProperty()));
151146
if (!method_exists($object, $get)) {
152-
throw new InvalidArgumentException(sprintf('Method "%s" is required in "%s" in order to be stored in "%s"', $get, get_class($object), get_class($this)), __LINE__);
147+
throw new \InvalidArgumentException(sprintf('Method "%s" is required in "%s" in order to be stored in "%s"', $get, get_class($object), get_class($this)), __LINE__);
153148
}
154149

155150
$key = $object->{$get}();
156151
if (!is_scalar($key)) {
157-
throw new InvalidArgumentException(sprintf('Property "%s" of "%s" must be scalar, "%s" returned', $this->objectProperty(), get_class($object), gettype($key)), __LINE__);
152+
throw new \InvalidArgumentException(sprintf('Property "%s" of "%s" must be scalar, "%s" returned', $this->objectProperty(), get_class($object), gettype($key)), __LINE__);
158153
}
159154

160155
return $key;

src/Container/Model/Struct.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ public function getVirtualKey(string $name): string
102102
* By overriding this method, we ensure that each time a new object is stored, it is stored with our new key if the inheritance is defined.
103103
*
104104
* @param Model $object
105-
*
106-
* @return Struct
107105
*/
108106
public function add(object $object): self
109107
{

src/File/AbstractModelFile.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace WsdlToPhp\PackageGenerator\File;
66

7-
use InvalidArgumentException;
87
use WsdlToPhp\PackageGenerator\ConfigurationReader\XsdTypes;
98
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant;
109
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method;
@@ -70,7 +69,7 @@ public function getDestinationFolder(bool $withSrc = true): string
7069
public function writeFile(bool $withSrc = true): void
7170
{
7271
if (!$this->getModel()) {
73-
throw new InvalidArgumentException('You MUST define the model before being able to generate the file', __LINE__);
72+
throw new \InvalidArgumentException('You MUST define the model before being able to generate the file', __LINE__);
7473
}
7574

7675
GeneratorUtils::createDirectory($this->getFileDestination($withSrc));
@@ -134,10 +133,10 @@ public function getStructAttributeType(StructAttributeModel $attribute = null, b
134133
$attribute = $this->getStructAttribute($attribute);
135134

136135
if (!$attribute instanceof StructAttributeModel) {
137-
throw new InvalidArgumentException('Could not find any valid StructAttribute');
136+
throw new \InvalidArgumentException('Could not find any valid StructAttribute');
138137
}
139138

140-
if ($returnArrayType && ($attribute->isArray())) {
139+
if ($returnArrayType && $attribute->isArray()) {
141140
return self::TYPE_ARRAY;
142141
}
143142

@@ -174,7 +173,7 @@ public function getStructAttributeTypeAsPhpType(StructAttributeModel $fromAttrib
174173
$attribute = $this->getStructAttribute($fromAttribute);
175174

176175
if (!$attribute instanceof StructAttributeModel) {
177-
throw new InvalidArgumentException('Could not find any valid StructAttribute');
176+
throw new \InvalidArgumentException('Could not find any valid StructAttribute');
178177
}
179178

180179
$attributeType = $this->getStructAttributeType($attribute, true, $returnArrayType);

src/File/AbstractOperation.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace WsdlToPhp\PackageGenerator\File;
66

7-
use InvalidArgumentException;
87
use WsdlToPhp\PackageGenerator\Generator\Generator;
98
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
109
use WsdlToPhp\PackageGenerator\Model\Method as MethodModel;
@@ -115,8 +114,8 @@ protected function getMethodParameter(string $name, ?string $type = null): PhpFu
115114
{
116115
try {
117116
return new PhpFunctionParameter($name, PhpFunctionParameter::NO_VALUE, $type);
118-
} catch (InvalidArgumentException $exception) {
119-
throw new InvalidArgumentException(sprintf('Unable to create function parameter for method "%s" with type "%s" and name "%s"', $this->getMethod()->getName(), var_export($type, true), $name), __LINE__, $exception);
117+
} catch (\InvalidArgumentException $exception) {
118+
throw new \InvalidArgumentException(sprintf('Unable to create function parameter for method "%s" with type "%s" and name "%s"', $this->getMethod()->getName(), var_export($type, true), $name), __LINE__, $exception);
120119
}
121120
}
122121

src/File/Service.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace WsdlToPhp\PackageGenerator\File;
66

7-
use InvalidArgumentException;
8-
use SoapFault;
97
use WsdlToPhp\PackageGenerator\ConfigurationReader\GeneratorOptions;
108
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
119
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer;
@@ -68,7 +66,7 @@ public static function getOperationMethodReturnType(MethodModel $method, Generat
6866
public function setModel(AbstractModel $model): self
6967
{
7068
if (!$model instanceof ServiceModel) {
71-
throw new InvalidArgumentException('Model must be an instance of a Service', __LINE__);
69+
throw new \InvalidArgumentException('Model must be an instance of a Service', __LINE__);
7270
}
7371

7472
return parent::setModel($model);
@@ -99,7 +97,7 @@ protected function getPropertyAnnotationBlock(PhpProperty $property): ?PhpAnnota
9997

10098
protected function defineUseStatements(): AbstractModelFile
10199
{
102-
$this->getFile()->addUse(SoapFault::class);
100+
$this->getFile()->addUse(\SoapFault::class);
103101

104102
/** @var Method $method */
105103
foreach ($this->getModel()->getMethods() as $method) {
@@ -116,7 +114,7 @@ protected function defineUseStatements(): AbstractModelFile
116114
continue;
117115
}
118116

119-
$this->getFile()->addUse(InvalidArgumentException::class);
117+
$this->getFile()->addUse(\InvalidArgumentException::class);
120118

121119
break 2;
122120
}
@@ -184,8 +182,8 @@ protected function getSoapHeaderMethod(string $methodName, string $soapHeaderNam
184182
$firstParameter->setModel($model);
185183
}
186184
$method->addChild(sprintf('return $this->%s($%s, \'%s\', $%s, $%s, $%s);', self::METHOD_SET_HEADER_PREFIX, self::PARAM_SET_HEADER_NAMESPACE, $soapHeaderName, lcfirst($soapHeaderName), self::PARAM_SET_HEADER_MUSTUNDERSTAND, self::PARAM_SET_HEADER_ACTOR));
187-
} catch (InvalidArgumentException $exception) {
188-
throw new InvalidArgumentException(sprintf('Unable to create function parameter for service "%s" with type "%s"', $this->getModel()->getName(), var_export($this->getTypeFromName($soapHeaderName), true)), __LINE__, $exception);
185+
} catch (\InvalidArgumentException $exception) {
186+
throw new \InvalidArgumentException(sprintf('Unable to create function parameter for service "%s" with type "%s"', $this->getModel()->getName(), var_export($this->getTypeFromName($soapHeaderName), true)), __LINE__, $exception);
189187
}
190188

191189
return $method;
@@ -260,7 +258,7 @@ protected function addAnnotationBlockForSoapHeaderMethod(PhpAnnotationBlock $ann
260258
$annotationBlock
261259
->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
262260
->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $firstParameter->getModel()->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
263-
->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, InvalidArgumentException::class))
261+
->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, \InvalidArgumentException::class))
264262
;
265263
}
266264
}

0 commit comments

Comments
 (0)