|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Deprecations; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PhpParser\Node\Name; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Broker\FunctionNotFoundException; |
| 10 | +use PHPStan\Php\PhpVersion; |
| 11 | +use PHPStan\Reflection\ReflectionProvider; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | +use function array_key_exists; |
| 15 | +use function count; |
| 16 | +use function in_array; |
| 17 | +use function sprintf; |
| 18 | +use function strtolower; |
| 19 | + |
| 20 | +/** |
| 21 | + * @implements Rule<FuncCall> |
| 22 | + */ |
| 23 | +class CallWithDeprecatedIniOptionRule implements Rule |
| 24 | +{ |
| 25 | + |
| 26 | + private const INI_FUNCTIONS = [ |
| 27 | + 'ini_get', |
| 28 | + 'ini_set', |
| 29 | + 'ini_alter', |
| 30 | + 'ini_restore', |
| 31 | + 'get_cfg_var', |
| 32 | + ]; |
| 33 | + |
| 34 | + private const DEPRECATED_OPTIONS = [ |
| 35 | + // deprecated since unknown version |
| 36 | + 'mbstring.http_input' => 0, |
| 37 | + 'mbstring.http_output' => 0, |
| 38 | + 'mbstring.internal_encoding' => 0, |
| 39 | + 'pdo_odbc.db2_instance_name' => 0, |
| 40 | + 'enable_dl' => 0, |
| 41 | + |
| 42 | + 'iconv.input_encoding' => 50600, |
| 43 | + 'iconv.output_encoding' => 50600, |
| 44 | + 'iconv.internal_encoding' => 50600, |
| 45 | + |
| 46 | + 'mbstring.func_overload' => 70200, |
| 47 | + 'track_errors' => 70200, |
| 48 | + |
| 49 | + 'allow_url_include' => 70400, |
| 50 | + |
| 51 | + 'assert.quiet_eval' => 80000, |
| 52 | + |
| 53 | + 'filter.default' => 80100, |
| 54 | + 'oci8.old_oci_close_semantics' => 80100, |
| 55 | + |
| 56 | + 'assert.active' => 80300, |
| 57 | + 'assert.exception' => 80300, |
| 58 | + 'assert.bail' => 80300, |
| 59 | + 'assert.warning' => 80300, |
| 60 | + |
| 61 | + 'session.sid_length' => 80400, |
| 62 | + 'session.sid_bits_per_character' => 80400, |
| 63 | + ]; |
| 64 | + |
| 65 | + private ReflectionProvider $reflectionProvider; |
| 66 | + |
| 67 | + private DeprecatedScopeHelper $deprecatedScopeHelper; |
| 68 | + |
| 69 | + private PhpVersion $phpVersion; |
| 70 | + |
| 71 | + public function __construct( |
| 72 | + ReflectionProvider $reflectionProvider, |
| 73 | + DeprecatedScopeHelper $deprecatedScopeHelper, |
| 74 | + PhpVersion $phpVersion |
| 75 | + ) |
| 76 | + { |
| 77 | + $this->reflectionProvider = $reflectionProvider; |
| 78 | + $this->deprecatedScopeHelper = $deprecatedScopeHelper; |
| 79 | + $this->phpVersion = $phpVersion; |
| 80 | + } |
| 81 | + |
| 82 | + public function getNodeType(): string |
| 83 | + { |
| 84 | + return FuncCall::class; |
| 85 | + } |
| 86 | + |
| 87 | + public function processNode(Node $node, Scope $scope): array |
| 88 | + { |
| 89 | + if ($this->deprecatedScopeHelper->isScopeDeprecated($scope)) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + if (!($node->name instanceof Name)) { |
| 94 | + return []; |
| 95 | + } |
| 96 | + |
| 97 | + if (count($node->getArgs()) < 1) { |
| 98 | + return []; |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + $function = $this->reflectionProvider->getFunction($node->name, $scope); |
| 103 | + } catch (FunctionNotFoundException $e) { |
| 104 | + // Other rules will notify if the function is not found |
| 105 | + return []; |
| 106 | + } |
| 107 | + |
| 108 | + if (!in_array(strtolower($function->getName()), self::INI_FUNCTIONS, true)) { |
| 109 | + return []; |
| 110 | + } |
| 111 | + |
| 112 | + $phpVersionId = $this->phpVersion->getVersionId(); |
| 113 | + $iniType = $scope->getType($node->getArgs()[0]->value); |
| 114 | + foreach ($iniType->getConstantStrings() as $string) { |
| 115 | + if (!array_key_exists($string->getValue(), self::DEPRECATED_OPTIONS)) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + if ($phpVersionId < self::DEPRECATED_OPTIONS[$string->getValue()]) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + return [ |
| 124 | + RuleErrorBuilder::message(sprintf( |
| 125 | + "Call to function %s() with deprecated option '%s'.", |
| 126 | + $function->getName(), |
| 127 | + $string->getValue(), |
| 128 | + ))->identifier('function.deprecated')->build(), |
| 129 | + ]; |
| 130 | + } |
| 131 | + |
| 132 | + return []; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments