-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature-rectore-3115670: rule for file_unmanaged_copy #43
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
/** | ||
* A simple example using the minimum required (all) number of arguments. | ||
*/ | ||
function simple_example() { | ||
$source = '/test/directory'; | ||
$destination = '/test/directory_2'; | ||
file_unmanaged_copy($source, $destination,FILE_CREATE_DIRECTORY); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace DrupalRector\Rector\Deprecation; | ||
|
||
use DrupalRector\Utility\TraitsByClassHelperTrait; | ||
use PhpParser\Node; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\RectorDefinition\CodeSample; | ||
use Rector\RectorDefinition\RectorDefinition; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mohsin-saeed, with the update to Rector, all of the Sorry for moving things from underneath you. :( |
||
|
||
/** | ||
* Replaces deprecated file_unmanaged_copy() calls. | ||
* | ||
* See https://api.drupal.org/api/drupal/core%21includes%21file.inc/function/file_unmanaged_copy/8.7.x. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide a link to the change record. This is usually in the function definition comment,
|
||
* | ||
* What is covered: | ||
* - File System Service used | ||
* | ||
* Improvement opportunities | ||
* - Dependency Injection | ||
*/ | ||
final class FileUnmanagedCopyRector extends AbstractRector { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For simple function to service method calls, we have a class you could extend, Unfortunately, I think that this rector rule is more complex. I'll add a comment to the overall review. |
||
use TraitsByClassHelperTrait; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless you are looking at traits for classes, you don't need to add this trait to the rector class. |
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getDefinition(): RectorDefinition { | ||
return new RectorDefinition('Fixes deprecated file_unmanaged_copy() calls', [ | ||
new CodeSample( | ||
<<<'CODE_BEFORE' | ||
file_unmanaged_copy($source, $destination, $replace); | ||
CODE_BEFORE | ||
, | ||
<<<'CODE_AFTER' | ||
\Drupal::service('file_system')->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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've started to use |
||
$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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohsin-saeed, it looks like you can actually call
file_unmanaged_copy
with just thesource
argument.Can you create an example with that?