{"record":{"id":"165c2b7fa2f2e5e9","repo":"rectorphp/rector","slug":"e-user-deprecated","errorCode":"E_USER_DEPRECATED","errorMessage":"UseNodesToAddCollector::%s is deprecated and will be removed. Use \"%s\" instead, via $file->getFileNode().","messagePattern":"UseNodesToAddCollector::(.+?) is deprecated and will be removed\\. Use \"(.+?)\" instead, via \\$file->getFileNode\\(\\)\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/PostRector/Collector/UseNodesToAddCollector.php","lineNumber":213,"sourceCode":"    {\n        $this->warn('getFunctionImportsByFilePath()', '$file->getFileNode()->getPendingImports()->getFunctionImports()');\n        $fileNode = $this->resolveCurrentFileNode();\n        if (!$fileNode instanceof FileNode) {\n            return [];\n        }\n        return $fileNode->getPendingImports()->getFunctionImports();\n    }\n    private function resolveCurrentFileNode(): ?FileNode\n    {\n        $file = $this->currentFileProvider->getFile();\n        if (!$file instanceof File) {\n            return null;\n        }\n        return $file->getFileNode();\n    }\n    private function warn(string $method, string $replacement): void\n    {\n        trigger_error(sprintf('UseNodesToAddCollector::%s is deprecated and will be removed. Use \"%s\" instead, via $file->getFileNode().', $method, $replacement), \\E_USER_DEPRECATED);\n    }\n}\n","sourceCodeStart":195,"sourceCodeEnd":216,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/src/PostRector/Collector/UseNodesToAddCollector.php#L195-L216","documentation":"An E_USER_DEPRECATED is triggered (UseNodesToAddCollector::warn(), src/PostRector/Collector/UseNodesToAddCollector.php:213) whenever custom Rector rule or extension code calls one of the collector's legacy import methods: addUseImport(), addConstantUseImport(), addFunctionUseImport(), getUseImportTypesByNode(), hasImport(), isShortImported(), isImportShortable(), getObjectImportsByFilePath(), getConstantImportsByFilePath() or getFunctionImportsByFilePath(). The import/import-manipulation state moved from this global collector onto the per-file FileNode / PendingImports object, reachable via $file->getFileNode(). The old methods still work (they delegate internally) but only emit this notice and will be removed.","triggerScenarios":"Writing a custom Rector rule that injects UseNodesToAddCollector and calls e.g. $this->useNodesToAddCollector->addUseImport($type) to queue a `use` import; calling hasImport()/isShortImported() in a rule's conditions; reading current imports via getObjectImportsByFilePath(); running third-party/custom rules built against an older rector/rector API during `vendor/bin/rector process` - each legacy call site logs one deprecation line (often surfaced by PHPUnit or an error_handler converting E_USER_DEPRECATED to exceptions).","commonSituations":"Maintaining private/company Rector rules that were written for Rector 1.x and running them under a newer version; CI setups with convertDeprecationsToExceptions=\"true\" in phpunit.xml or a strict error handler that turns the notice into a failing test; copied rule code from old blog posts or the Rector playbook using the collector to add imports.","solutions":["Refactor the call site to the FileNode API named in the message: get the current file via CurrentFileProvider (or the rule's refected file) and call $file->getFileNode()->getPendingImports()->addUseImport($type) (or addConstantUseImport/addFunctionUseImport) for writes.","For read-style calls use the direct replacements: hasImport($type) -> $fileNode->hasImport($type); getUseImportTypesByNode() -> $fileNode->resolveUsedImportTypes(); isShortImported()/isImportShortable()/get*ImportsByFilePath() -> the matching getPendingImports() methods on FileNode.","If the call comes from a third-party rule, check for an upgraded release of that package; otherwise pin rector/rector to the version the rule was built for until it is ported.","If you must keep CI green while migrating, temporarily silence E_USER_DEPRECATED (error_reporting(E_ALL & ~E_USER_DEPRECATED) or PHPUnit convertDeprecationsToExceptions=\"false\") - but treat it as a stopgap, the methods will be removed."],"exampleFix":"// before\nuse Rector\\PostRector\\Collector\\UseNodesToAddCollector;\n\n$this->useNodesToAddCollector->addUseImport($className); // triggers E_USER_DEPRECATED\n\n// after\nuse Rector\\PostRector\\Collector\\UseNodesToAddCollector;\nuse Rector\\Application\\FileProcessor; // not needed; use current file provider instead\n$file = $this->currentFileProvider->getFile();\n$file->getFileNode()->getPendingImports()->addUseImport($className);","handlingStrategy":"validation","validationCode":"// detect deprecated collector calls before running Rector, e.g. as a CI grep/PHPStan gate\n$deprecated = [\n    'addUseImport', 'addConstantUseImport', 'addFunctionUseImport',\n    'getUseImportTypesByNode', 'hasImport', 'isShortImported', 'isImportShortable',\n    'getObjectImportsByFilePath', 'getConstantImportsByFilePath', 'getFunctionImportsByFilePath',\n];\n// quick gate over custom rules:\nforeach (glob('src/*Rector.php') as $file) {\n    $code = file_get_contents($file);\n    foreach ($deprecated as $method) {\n        if (str_contains($code, 'useNodesToAddCollector->' . $method)) {\n            fwrite(STDERR, \"{$file} uses deprecated UseNodesToAddCollector::{$method}()\\n\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"// only as a stopgap while migrating: keep deprecations from failing PHPUnit/CI\nset_error_handler(function (int $errno, string $errstr): bool {\n    if ($errno === E_USER_DEPRECATED && str_contains($errstr, 'UseNodesToAddCollector::')) {\n        return true; // suppress; log $errstr if needed\n    }\n    return false; // default handler\n});","preventionTips":["In custom rules, never touch UseNodesToAddCollector directly: reach imports through $file->getFileNode() / getPendingImports(), which is the supported API.","Enable phpstan/phpstan-deprecation-rules (or a CI grep) on your custom Rector rules so deprecated calls fail statically, before they emit notices at runtime.","Keep phpunit.xml convertDeprecationsToExceptions in sync with your migration state: strict while porting rules off the collector, so regressions surface immediately.","When upgrading rector/rector major versions, run your rule test suite first and fix every UseNodesToAddCollector deprecation it reports."],"tags":["rector","deprecation","e-user-deprecated","imports","custom-rules","php"],"backgroundTag":"deprecated-method-call","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}