From 49f1233c5f39a7fc34c49a5f38f6686ddd0a4b0f Mon Sep 17 00:00:00 2001 From: Mohsin Saeed Date: Fri, 17 Apr 2020 18:31:43 +0300 Subject: [PATCH] feature-rectore-3115670: rule for file_unmanaged_copy --- config/drupal-8/drupal-8.7-deprecations.yml | 1 + deprecation-index.yml | 5 ++ rector_examples/file_unmanaged_copy.php | 10 +++ .../Deprecation/FileUnmanagedCopyRector.php | 71 +++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 rector_examples/file_unmanaged_copy.php create mode 100644 src/Rector/Deprecation/FileUnmanagedCopyRector.php diff --git a/config/drupal-8/drupal-8.7-deprecations.yml b/config/drupal-8/drupal-8.7-deprecations.yml index 2ce75447..7a9288b7 100644 --- a/config/drupal-8/drupal-8.7-deprecations.yml +++ b/config/drupal-8/drupal-8.7-deprecations.yml @@ -2,3 +2,4 @@ services: DrupalRector\Rector\Deprecation\FilePrepareDirectoryRector: ~ DrupalRector\Rector\Deprecation\FileCreateDirectoryRector: ~ + DrupalRector\Rector\Deprecation\FileUnmanagedCopyRector: ~ diff --git a/deprecation-index.yml b/deprecation-index.yml index 03af9c74..8c0881a1 100644 --- a/deprecation-index.yml +++ b/deprecation-index.yml @@ -129,3 +129,8 @@ - DrupalRenderRootStaticUpdated.php - drupal_render_root.php - drupal_render_root_updated.php +'file_unmanaged_copy()': + Rector: FileUnmanagedCopyRector.php + PHPStan: 'Call to deprecated function file_unmanaged_copy(). Deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::copy().' + Examples: + - file_unmanaged_copy.php \ No newline at end of file diff --git a/rector_examples/file_unmanaged_copy.php b/rector_examples/file_unmanaged_copy.php new file mode 100644 index 00000000..e08068bc --- /dev/null +++ b/rector_examples/file_unmanaged_copy.php @@ -0,0 +1,10 @@ +copy($source, $destination, $replace); +CODE_AFTER + ), + ]); + } + + /** + * @inheritdoc + */ + public function getNodeTypes(): array { + return [ + Node\Expr\FuncCall::class, + ]; + } + + /** + * @inheritdoc + */ + public function refactor(Node $node): ?Node { + if ($node->name instanceof Node\Name && 'file_unmanaged_copy' === (string) $node->name) { + $file_system_service = new Node\Expr\StaticCall(new Node\Name\FullyQualified('Drupal'), 'service', [new Node\Arg(new Node\Scalar\String_('file_system'))]); + + $method_name = 'copy'; + + $method = new Node\Identifier($method_name); + + $node = new Node\Expr\MethodCall($file_system_service, $method, $node->args); + + return $node; + + } + + return NULL; + } + +}