file_provider = new FakeFileProvider(); $config = new TestConfig(); $providers = new Providers( $this->file_provider, new ParserInstanceCacheProvider(), null, null, new FakeFileReferenceCacheProvider(), new ProjectCacheProvider(), ); $this->project_analyzer = new ProjectAnalyzer( $config, $providers, ); $this->project_analyzer->setPhpVersion('7.3', 'tests'); } /** * @dataProvider providerTestInvalidUpdates * @param array> $file_stages * @param array $ignored_issues */ public function testErrorAfterUpdate( array $file_stages, string $error_message, array $ignored_issues = [] ): void { $this->project_analyzer->getCodebase()->diff_methods = true; $this->project_analyzer->getCodebase()->reportUnusedCode(); $codebase = $this->project_analyzer->getCodebase(); $config = $codebase->config; foreach ($ignored_issues as $error_type => $error_level) { $config->setCustomErrorLevel($error_type, $error_level); } if (!$file_stages) { throw new UnexpectedValueException('$file_stages should not be empty'); } $end_files = array_pop($file_stages); foreach ($file_stages as $files) { foreach ($files as $file_path => $contents) { $this->file_provider->registerFile($file_path, $contents); } $codebase->reloadFiles($this->project_analyzer, array_keys($files)); $codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true); } foreach ($end_files as $file_path => $contents) { $this->file_provider->registerFile($file_path, $contents); } $this->expectException(CodeException::class); $this->expectExceptionMessageMatches('/\b' . preg_quote($error_message, '/') . '\b/'); $codebase->reloadFiles($this->project_analyzer, array_keys($end_files)); foreach ($end_files as $file_path => $_) { $codebase->addFilesToAnalyze([$file_path => $file_path]); } $codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true); } /** * @return array>,error_message:string}> */ public function providerTestInvalidUpdates(): array { return [ 'invalidateParentCaller' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'foo(); } } (new C())->bar();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'foo(); } } (new C())->bar();', ], ], 'error_message' => 'UndefinedMethod', ], 'invalidateAfterPropertyTypeChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo; } } echo (new B)->foo();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo; } } echo (new B)->foo();', ], ], 'error_message' => 'InvalidReturnStatement', ], 'invalidateAfterConstantChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo();', ], ], 'error_message' => 'InvalidReturnStatement', ], 'invalidateAfterSkippedAnalysis' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'getB()->getString(); } } echo (new C)->existingMethod();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'getB()->getString(); } public function newMethod() : void {} } echo (new C)->existingMethod(); // newly-added call, removed in the next code block (new C)->newMethod();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'getB()->getString(); } } echo (new C)->existingMethod();', ], ], 'error_message' => 'NullableReturnStatement', ], 'invalidateMissingConstructorAfterPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo;', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo;', ], ], 'error_message' => 'MissingConstructor', ], 'invalidateEmptyConstructorAfterPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo;', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo;', ], ], 'error_message' => 'PropertyNotSetInConstructor', ], 'invalidateEmptyTraitConstructorAfterPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo;', getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => ' 'foo;', getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => ' 'PropertyNotSetInConstructor', ], 'invalidateEmptyTraitConstructorAfterTraitPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo;', getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => ' 'foo;', getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => ' 'PropertyNotSetInConstructor', ], 'invalidateSetInPrivateMethodConstructorCheck' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'setFoo(); } private function setFoo() : void { $this->foo = "bar"; } } echo (new A)->foo;', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'setFoo(); } private function setFoo() : void { } } echo (new A)->foo;', ], ], 'error_message' => 'PropertyNotSetInConstructor', ], 'invalidateMissingConstructorAfterParentPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo;', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo;', ], ], 'error_message' => 'MissingConstructor', ], 'invalidateNotSetInConstructorAfterParentPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo;', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo;', ], ], 'error_message' => 'PropertyNotSetInConstructor', ], 'duplicateClass' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'DuplicateClass', ], 'duplicateMethod' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'DuplicateMethod', ], 'unusedClassReferencedInFile' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'UnusedClass', ], 'unusedMethodReferencedInFile' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'PossiblyUnusedMethod', ], 'unusedStaticMethodReferencedInFile' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' ' ' ' 'PossiblyUnusedMethod', ], 'unusedParamReferencedInFile' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo("hello");', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo("hello");', ], ], 'error_message' => 'PossiblyUnusedParam', ], 'unusedMethodReferencedInMethod' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo(); } } (new B)->bar();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'bar();', ], ], 'error_message' => 'PossiblyUnusedMethod', ], 'unusedPropertyReferencedInFile' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo;', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => ' 'PossiblyUnusedProperty', ], 'unusedPropertyReferencedInMethod' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'foo; } } (new B)->bar();', ], [ getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => 'bar();', ], ], 'error_message' => 'PossiblyUnusedProperty', ], 'uninitialisedChildProperty' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => 'setFoo(); } abstract protected function setFoo() : void; }', getcwd() . DIRECTORY_SEPARATOR . 'AChild.php' => 'reallySetFoo(); } private function reallySetFoo() : void { $this->foo = "bar"; } }', ], [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => 'setFoo(); } abstract protected function setFoo() : void; }', getcwd() . DIRECTORY_SEPARATOR . 'AChild.php' => 'reallySetFoo(); } private function reallySetFoo() : void { //$this->foo = "bar"; } }', ], ], 'error_message' => 'PropertyNotSetInConstructor', ], 'invalidateChildMethodWhenSignatureChanges' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'MethodSignatureMismatch', ], ]; } }