| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Reflection; |
| 4: | |
| 5: | use PHPStan\TrinaryLogic; |
| 6: | use PHPStan\Turbo\ShadowedByTurboExtension; |
| 7: | use function in_array; |
| 8: | use function str_starts_with; |
| 9: | use function strlen; |
| 10: | use function strpos; |
| 11: | use function substr; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/SideEffects.cpp')] |
| 30: | final class SideEffects |
| 31: | { |
| 32: | |
| 33: | public const WRITES_THIS = 'writes-this'; |
| 34: | public const WRITES_ARGS = 'writes-args'; |
| 35: | public const WRITES_GLOBAL = 'writes-global'; |
| 36: | public const READS_GLOBAL = 'reads-global'; |
| 37: | public const NONDETERMINISTIC = 'nondeterministic'; |
| 38: | public const NONDETERMINISTIC_TIME = 'nondeterministic.time'; |
| 39: | public const NONDETERMINISTIC_RANDOM = 'nondeterministic.random'; |
| 40: | public const IO = 'io'; |
| 41: | public const IO_OUTPUT = 'io.output'; |
| 42: | public const IO_FILES = 'io.files'; |
| 43: | public const IO_NETWORK = 'io.network'; |
| 44: | public const IO_DB = 'io.db'; |
| 45: | public const IO_PROCESS = 'io.process'; |
| 46: | public const SLEEP = 'sleep'; |
| 47: | public const EXIT = 'exit'; |
| 48: | public const WARNS = 'warns'; |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public const WRITES_GLOBAL_STAT_CACHE = 'writes-global.stat-cache'; |
| 55: | public const READS_GLOBAL_STAT_CACHE = 'reads-global.stat-cache'; |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public const WRITES_GLOBAL_MBSTRING = 'writes-global.mbstring'; |
| 63: | public const READS_GLOBAL_MBSTRING = 'reads-global.mbstring'; |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public const WRITES_GLOBAL_ERROR = 'writes-global.error'; |
| 70: | public const WRITES_GLOBAL_ERROR_JSON = 'writes-global.error.json'; |
| 71: | public const READS_GLOBAL_ERROR_JSON = 'reads-global.error.json'; |
| 72: | public const WRITES_GLOBAL_ERROR_PREG = 'writes-global.error.preg'; |
| 73: | public const READS_GLOBAL_ERROR_PREG = 'reads-global.error.preg'; |
| 74: | public const WRITES_GLOBAL_ERROR_OPENSSL = 'writes-global.error.openssl'; |
| 75: | public const READS_GLOBAL_ERROR_OPENSSL = 'reads-global.error.openssl'; |
| 76: | public const WRITES_GLOBAL_ERROR_INTL = 'writes-global.error.intl'; |
| 77: | public const READS_GLOBAL_ERROR_INTL = 'reads-global.error.intl'; |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | private function __construct( |
| 85: | private array $effects, |
| 86: | private TrinaryLogic $others, |
| 87: | private array $calledParameters, |
| 88: | private array $writtenParameters, |
| 89: | private bool $readsWrittenArguments, |
| 90: | ) |
| 91: | { |
| 92: | } |
| 93: | |
| 94: | public static function none(): self |
| 95: | { |
| 96: | return new self([], TrinaryLogic::createNo(), [], [], false); |
| 97: | } |
| 98: | |
| 99: | public static function all(): self |
| 100: | { |
| 101: | return new self([], TrinaryLogic::createYes(), [], [], false); |
| 102: | } |
| 103: | |
| 104: | public static function unknown(): self |
| 105: | { |
| 106: | return new self([], TrinaryLogic::createMaybe(), [], [], false); |
| 107: | } |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | public static function create(array $effects, TrinaryLogic $others, array $calledParameters = [], array $writtenParameters = [], bool $readsWrittenArguments = false): self |
| 119: | { |
| 120: | return new self($effects, $others, $calledParameters, $writtenParameters, $readsWrittenArguments); |
| 121: | } |
| 122: | |
| 123: | |
| 124: | public function hasAny(): TrinaryLogic |
| 125: | { |
| 126: | if ($this->effects !== []) { |
| 127: | return TrinaryLogic::createYes(); |
| 128: | } |
| 129: | |
| 130: | if ($this->others->no() && ($this->calledParameters !== [] || $this->writtenParameters !== [])) { |
| 131: | return TrinaryLogic::createMaybe(); |
| 132: | } |
| 133: | |
| 134: | return $this->others; |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | public function has(string $effect): TrinaryLogic |
| 142: | { |
| 143: | $result = $this->others; |
| 144: | foreach ($this->effects as $listed) { |
| 145: | $listed = self::baseName($listed); |
| 146: | if ($listed === $effect || str_starts_with($listed, $effect . '.')) { |
| 147: | return TrinaryLogic::createYes(); |
| 148: | } |
| 149: | if (!str_starts_with($effect, $listed . '.')) { |
| 150: | continue; |
| 151: | } |
| 152: | |
| 153: | $result = $result->or(TrinaryLogic::createMaybe()); |
| 154: | } |
| 155: | |
| 156: | return $result; |
| 157: | } |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | public function isDeterministic(): TrinaryLogic |
| 167: | { |
| 168: | if ($this->readsWrittenArguments) { |
| 169: | return TrinaryLogic::createNo(); |
| 170: | } |
| 171: | |
| 172: | return $this->has(self::NONDETERMINISTIC)->negate(); |
| 173: | } |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | public function hasOnly(array $effects): TrinaryLogic |
| 186: | { |
| 187: | if ($this->others->yes()) { |
| 188: | return TrinaryLogic::createNo(); |
| 189: | } |
| 190: | |
| 191: | $result = $this->others->negate(); |
| 192: | foreach ($this->effects as $listed) { |
| 193: | $listed = self::baseName($listed); |
| 194: | $listedResult = TrinaryLogic::createNo(); |
| 195: | foreach ($effects as $effect) { |
| 196: | if ($listed === $effect || str_starts_with($listed, $effect . '.')) { |
| 197: | $listedResult = TrinaryLogic::createYes(); |
| 198: | break; |
| 199: | } |
| 200: | if (!str_starts_with($effect, $listed . '.')) { |
| 201: | continue; |
| 202: | } |
| 203: | |
| 204: | $listedResult = TrinaryLogic::createMaybe(); |
| 205: | } |
| 206: | |
| 207: | $result = $result->and($listedResult); |
| 208: | } |
| 209: | |
| 210: | if (!in_array(self::WRITES_ARGS, $effects, true)) { |
| 211: | foreach ($this->writtenParameters as $written) { |
| 212: | $result = $result->and($written->negate()); |
| 213: | } |
| 214: | } |
| 215: | |
| 216: | return $result; |
| 217: | } |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | public function getEffectNames(): array |
| 226: | { |
| 227: | return $this->effects; |
| 228: | } |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | public function getListedSubEffects(string $effect): ?array |
| 239: | { |
| 240: | $subEffects = []; |
| 241: | foreach ($this->effects as $listed) { |
| 242: | $listed = self::baseName($listed); |
| 243: | if ($listed === $effect) { |
| 244: | return null; |
| 245: | } |
| 246: | if (!str_starts_with($listed, $effect . '.')) { |
| 247: | continue; |
| 248: | } |
| 249: | |
| 250: | $subEffects[] = substr($listed, strlen($effect) + 1); |
| 251: | } |
| 252: | |
| 253: | return $subEffects; |
| 254: | } |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | public function getCalledParameters(): array |
| 260: | { |
| 261: | return $this->calledParameters; |
| 262: | } |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | public function getWrittenParameters(): array |
| 268: | { |
| 269: | return $this->writtenParameters; |
| 270: | } |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | public function writesParameter(string $parameterName): TrinaryLogic |
| 278: | { |
| 279: | if (isset($this->writtenParameters[$parameterName])) { |
| 280: | return $this->writtenParameters[$parameterName]; |
| 281: | } |
| 282: | |
| 283: | return in_array(self::WRITES_ARGS, $this->effects, true) ? TrinaryLogic::createYes() : $this->others; |
| 284: | } |
| 285: | |
| 286: | |
| 287: | private static function baseName(string $listed): string |
| 288: | { |
| 289: | $position = strpos($listed, '('); |
| 290: | |
| 291: | return $position === false ? $listed : substr($listed, 0, $position); |
| 292: | } |
| 293: | |
| 294: | } |
| 295: | |