file_provider = new \Psalm\Tests\Provider\FakeFileProvider(); $config = new TestConfig(); $providers = new Providers( $this->file_provider, new \Psalm\Tests\Provider\ParserInstanceCacheProvider(), null, null, new Provider\FakeFileReferenceCacheProvider() ); $this->project_checker = new ProjectChecker( $config, $providers, false, true, ProjectChecker::TYPE_CONSOLE, 1, false ); $this->project_checker->infer_types_from_usage = true; } /** * @dataProvider providerTestInvalidUpdates * * @param array> $file_stages * @param array $error_levels * * @return void */ public function testErrorAfterUpdate( array $file_stages, string $error_message, array $error_levels = [] ) { $this->project_checker->diff_methods = true; $codebase = $this->project_checker->getCodebase(); $config = $codebase->config; foreach ($error_levels as $error_type => $error_level) { $config->setCustomErrorLevel($error_type, $error_level); } $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_checker, array_keys($files)); foreach ($files as $file_path => $contents) { $this->file_provider->registerFile($file_path, $contents); $codebase->addFilesToAnalyze([$file_path => $file_path]); } $codebase->scanFiles(); $codebase->analyzer->analyzeFiles($this->project_checker, 1, false); } foreach ($end_files as $file_path => $contents) { $this->file_provider->registerFile($file_path, $contents); } $codebase->reloadFiles($this->project_checker, array_keys($end_files)); foreach ($end_files as $file_path => $_) { $codebase->addFilesToAnalyze([$file_path => $file_path]); } $codebase->scanFiles(); $this->expectException('\Psalm\Exception\CodeException'); $this->expectExceptionMessageRegexp('/\b' . preg_quote($error_message, '/') . '\b/'); $codebase->analyzer->analyzeFiles($this->project_checker, 1, false); } /** * @return array */ public function providerTestInvalidUpdates() { return [ 'invalidateParentCaller' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'foo(); } }', ], [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'foo(); } }', ], ], 'error_message' => 'UndefinedMethod', ], 'invalidateAfterPropertyTypeChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo; } }', ], [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' 'foo; } }', ], ], 'error_message' => 'InvalidReturnStatement' ], 'invalidateAfterConstantChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' ' ' 'InvalidReturnStatement' ], 'invalidateAfterSkippedAnalysis' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'getB()->getString(); } }', ], [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'getB()->getString(); } public function bat() : void {} }', ], [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'getB()->getString(); } }', ], ], 'error_message' => 'NullableReturnStatement' ], 'invalidateMissingConstructorAfterPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'MissingConstructor' ], 'invalidateEmptyConstructorAfterPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' 'PropertyNotSetInConstructor' ], 'invalidateEmptyTraitConstructorAfterPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' ' ' 'PropertyNotSetInConstructor' ], 'invalidateEmptyTraitConstructorAfterTraitPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' ' ' 'PropertyNotSetInConstructor' ], 'invalidateSetInPrivateMethodConstructorCheck' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => 'setFoo(); } private function setFoo() : void { $this->foo = "bar"; } }', ], [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => 'setFoo(); } private function setFoo() : void { } }', ], ], 'error_message' => 'PropertyNotSetInConstructor' ], 'invalidateMissingConstructorAfterParentPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' ' ' 'MissingConstructor' ], 'invalidateNotSetInConstructorAfterParentPropertyChange' => [ 'file_stages' => [ [ getcwd() . DIRECTORY_SEPARATOR . 'A.php' => ' ' ' ' 'PropertyNotSetInConstructor' ], ]; } }