Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ItemTypeRule
92.31% covered (success)
92.31%
12 / 13
75.00% covered (warning)
75.00%
3 / 4
11.06
0.00% covered (danger)
0.00%
0 / 1
 name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 testConditions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 exceptionMessageOnTestFailure
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getItemSanityCheck
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
8.06
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7use WsdlToPhp\PackageGenerator\Model\Struct;
8
9final class ItemTypeRule extends AbstractRule
10{
11    public function name(): string
12    {
13        return 'itemType';
14    }
15
16    public function testConditions(string $parameterName, $value, bool $itemType = false): string
17    {
18        return sprintf('%s', $this->getItemSanityCheck($parameterName));
19    }
20
21    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
22    {
23        return sprintf('sprintf(\'The %1$s property can only contain items of type %2$s, %%s given\', is_object($%3$s) ? get_class($%3$s) : (is_array($%3$s) ? implode(\', \', $%3$s) : gettype($%3$s)))', $this->getAttribute()->getCleanName(), $this->getFile()->getStructAttributeTypeAsPhpType($this->getAttribute(), false), $parameterName);
24    }
25
26    /**
27     * The second case which used PHP native functions is voluntarily limited by the native functions provided by PHP,
28     * and the possible types defined in xsd_types.yml.
29     */
30    protected function getItemSanityCheck(string $itemName): string
31    {
32        $model = $this->getFile()->getModelFromStructAttribute($this->getAttribute());
33        $sanityCheck = 'false';
34        if ($model instanceof Struct && !$model->isList() && ($model->isStruct() || ($model->isArray() && $model->getInheritanceStruct() instanceof Struct))) {
35            $sanityCheck = sprintf('!$%s instanceof %s', $itemName, $this->getFile()->getStructAttributeTypeAsPhpType($this->getAttribute(), false));
36        } elseif ($this->getAttribute()->isXml()) {
37            $sanityCheck = $this->getRules()->getXmlRule()->testConditions($itemName, null, true);
38        } else {
39            $type = $this->getFile()->getStructAttributeTypeAsPhpType($this->getAttribute(), false);
40            if ($rule = $this->getRules()->getRule($type)) {
41                $sanityCheck = $rule->testConditions($itemName, null, true);
42            }
43        }
44
45        return $sanityCheck;
46    }
47}