Avoid unused local variables such as '$handled'. Open
$handled = "all";
- Read upRead up
- Exclude checks
UnusedLocalVariable
Since: 0.2
Detects when a local variable is declared and/or assigned, but not used.
Example
class Foo {
public function doSomething()
{
$i = 5; // Unused
}
}
Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable
Return type of actionClearAll()
is undeclared type \yii\web\Response
Open
public function actionClearAll()
- Exclude checks
Reference to static property app
from undeclared class \Craft
Open
if (Craft::$app->getRequest()->getParam('redirect')) {
- Exclude checks
Call to undeclared method \studioespresso\seofields\controllers\NotFoundController::asJson
Open
return $this->asJson(['success' => true]);
- Exclude checks
Call to undeclared method \studioespresso\seofields\controllers\NotFoundController::renderTemplate
Open
return $this->renderTemplate('seo-fields/_notfound/_index');
- Exclude checks
Reference to static property app
from undeclared class \Craft
Open
if (Craft::$app->getRequest()->getParam('redirect') === "handled") {
- Exclude checks
Reference to undeclared property \studioespresso\seofields\controllers\NotFoundController->request
(Did you mean $_REQUEST) Open
$id = $this->request->getBodyParam('id');
- Exclude checks
Call to method getInstance
from undeclared class \studioespresso\seofields\SeoFields
Open
SeoFields::getInstance()->notFoundService->deleteAll();
- Exclude checks
Reference to static property app
from undeclared class \Craft
Open
Craft::$app->getSession()->setNotice(Craft::t('seo-fields', '404 removed'));
- Exclude checks
Call to method cpUrl
from undeclared class \craft\helpers\UrlHelper
Open
return $this->redirect(UrlHelper::cpUrl('seo-fields/not-found'));
- Exclude checks
Class extends undeclared class \craft\web\Controller
Open
class NotFoundController extends Controller
- Exclude checks
Return type of actionIndex()
is undeclared type \yii\web\Response
Open
public function actionIndex($siteHandle = null)
- Exclude checks
Saw an @param annotation for id,
but the param list of function actionDelete()
is empty Open
* @param $id
- Exclude checks
Call to method getInstance
from undeclared class \studioespresso\seofields\SeoFields
Open
if (SeoFields::getInstance()->notFoundService->deletetById($id)) {
- Exclude checks
Reference to static property app
from undeclared class \Craft
Open
$site = Craft::$app->getSites()->getSiteByHandle($siteHandle);
- Exclude checks
Reference to static property app
from undeclared class \Craft
Open
} elseif (Craft::$app->getRequest()->getParam('redirect') === "not-handled") {
- Exclude checks
Reference to static property app
from undeclared class \Craft
Open
Craft::$app->getSites()->setCurrentSite($site);
- Exclude checks
Call to undeclared method \studioespresso\seofields\controllers\NotFoundController::redirect
Open
return $this->redirect(UrlHelper::cpUrl('seo-fields/not-found'));
- Exclude checks
@throws type of actionDelete
has undeclared type \craft\errors\MissingComponentException
Open
public function actionDelete()
- Exclude checks
Call to method t
from undeclared class \Craft
Open
Craft::$app->getSession()->setNotice(Craft::t('seo-fields', '404 removed'));
- Exclude checks
Avoid using static access to class '\Craft' in method 'actionDelete'. Open
Craft::$app->getSession()->setNotice(Craft::t('seo-fields', '404 removed'));
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Avoid using static access to class '\craft\helpers\UrlHelper' in method 'actionClearAll'. Open
return $this->redirect(UrlHelper::cpUrl('seo-fields/not-found'));
- Read upRead up
- Exclude checks
StaticAccess
Since: 1.4.0
Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.
Example
class Foo
{
public function bar()
{
Bar::baz();
}
}
Source https://phpmd.org/rules/cleancode.html#staticaccess
Define a constant instead of duplicating this literal "redirect" 3 times. Open
if (Craft::$app->getRequest()->getParam('redirect')) {
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Avoid variables with short names like $id. Configured minimum length is 3. Open
$id = $this->request->getBodyParam('id');
- Read upRead up
- Exclude checks
ShortVariable
Since: 0.2
Detects when a field, local, or parameter has a very short name.
Example
class Something {
private $q = 15; // VIOLATION - Field
public static function main( array $as ) { // VIOLATION - Formal
$r = 20 + $this->q; // VIOLATION - Local
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
$r += $this->q;
}
}
}