Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/Rector/RenameParameterRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

use function count;
use function is_array;
use function str_contains;

/** @psalm-suppress PropertyNotSetInConstructor */
class RenameParameterRector extends AbstractRector implements ConfigurableRectorInterface
Expand Down Expand Up @@ -81,15 +82,36 @@
return null;
}

// Get the class name - can be a string or Identifier node
$className = $node->name instanceof Node
? ($node->name->name ?? '')

Check failure on line 87 in src/Rector/RenameParameterRector.php

View workflow job for this annotation

GitHub Actions / Static analysis

Property PhpParser\Node\Identifier::$name (string) on left side of ?? is not nullable.
: ($node->name ?? '');

$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if ($classReflection === null) {
return null;

$shouldProcess = false;

// If reflection succeeds, verify the class is a Controller or Settings
if ($classReflection !== null) {
$extendsController = $classReflection->is('OCP\AppFramework\Controller');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also OCSController?

$implementsSettingsInterface = $classReflection->implementsInterface('OCP\Settings\ISettings');

if ($extendsController || $implementsSettingsInterface) {
$shouldProcess = true;
}
}

$extendsController = $classReflection->is('OCP\AppFramework\Controller');
$implementsSettingsInterface = $classReflection->implementsInterface('OCP\Settings\ISettings');
// If reflection failed or didn't match, use class name as fallback heuristic
// This allows the rule to work when analyzing apps in isolation where
// Nextcloud core classes aren't available or don't have parent classes declared
if (!$shouldProcess) {
// Only process if class name contains 'Controller' or 'Settings'
if (str_contains($className, 'Controller') || str_contains($className, 'Settings')) {
$shouldProcess = true;
}
}

if (!$extendsController && !$implementsSettingsInterface) {
if (!$shouldProcess) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

// Simulate an app in isolation where Controller class is NOT available
// (i.e., OCP namespace is not resolved)

namespace Nextcloud\Rector\Test\Rector\RenameParameterRector\Fixture;

class AccountsController
{
private string $currentUserId;

public function __construct(string $appName, $UserId) {
$this->currentUserId = $UserId;
}
}

?>
-----
<?php

// Simulate an app in isolation where Controller class is NOT available
// (i.e., OCP namespace is not resolved)

namespace Nextcloud\Rector\Test\Rector\RenameParameterRector\Fixture;

class AccountsController
{
private string $currentUserId;

public function __construct(string $appName, $userId) {
$this->currentUserId = $userId;
}
}

?>
Loading