|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SzepeViktor\PHPStan\WordPress; |
| 6 | + |
| 7 | +use PhpParser\Node\Expr\FuncCall; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\FunctionReflection; |
| 10 | +use PHPStan\Type\ArrayType; |
| 11 | +use PHPStan\Type\Constant\ConstantArrayType; |
| 12 | +use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
| 13 | +use PHPStan\Type\Constant\ConstantStringType; |
| 14 | +use PHPStan\Type\IntersectionType; |
| 15 | +use PHPStan\Type\MixedType; |
| 16 | +use PHPStan\Type\StringType; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\TypeTraverser; |
| 19 | +use PHPStan\Type\UnionType; |
| 20 | + |
| 21 | +class WpSlashDynamicFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension |
| 22 | +{ |
| 23 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 24 | + { |
| 25 | + return in_array( |
| 26 | + $functionReflection->getName(), |
| 27 | + [ |
| 28 | + 'addslashes_gpc', |
| 29 | + 'wp_slash', |
| 30 | + ], |
| 31 | + true |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @see https://developer.wordpress.org/reference/functions/addslashes_gpc/ |
| 37 | + * @see https://developer.wordpress.org/reference/functions/wp_slash/ |
| 38 | + */ |
| 39 | + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type |
| 40 | + { |
| 41 | + if (count($functionCall->getArgs()) === 0) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + $argType = $scope->getType($functionCall->getArgs()[0]->value); |
| 46 | + |
| 47 | + $accepted = new UnionType( |
| 48 | + [ |
| 49 | + new StringType(), |
| 50 | + new ArrayType(new MixedType(), new MixedType()), |
| 51 | + ] |
| 52 | + ); |
| 53 | + |
| 54 | + if (! $accepted->isSuperTypeOf($argType)->yes()) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + return $this->addSlashes($argType); |
| 59 | + } |
| 60 | + |
| 61 | + private function addSlashes(Type $type): Type |
| 62 | + { |
| 63 | + return TypeTraverser::map( |
| 64 | + $type, |
| 65 | + function (Type $type, callable $traverse): Type { |
| 66 | + if ($type instanceof UnionType || $type instanceof IntersectionType) { |
| 67 | + return $traverse($type); |
| 68 | + } |
| 69 | + |
| 70 | + if ($type instanceof ConstantStringType) { |
| 71 | + return new ConstantStringType(addslashes($type->getValue())); |
| 72 | + } |
| 73 | + |
| 74 | + if (! $type->isArray()->yes()) { |
| 75 | + return $type; |
| 76 | + } |
| 77 | + |
| 78 | + if (! ($type instanceof ConstantArrayType)) { |
| 79 | + return new ArrayType( |
| 80 | + $type->getIterableKeyType(), |
| 81 | + $this->addSlashes($type->getIterableValueType()) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + $builder = ConstantArrayTypeBuilder::createEmpty(); |
| 86 | + |
| 87 | + foreach ($type->getKeyTypes() as $index => $keyType) { |
| 88 | + $builder->setOffsetValueType( |
| 89 | + $keyType, |
| 90 | + $this->addSlashes($type->getValueTypes()[$index]), |
| 91 | + $type->isOptionalKey($index) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return $builder->getArray(); |
| 96 | + } |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments