Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PHPMD files#47

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
nimratcoderabbit wants to merge1 commit intomain
base:main
Choose a base branch
Loading
frompreview/phpmd
Open
Changes fromall 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
40 changes: 40 additions & 0 deletionsbad.php
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
<?php

class BadDesign
{
// No class doc comment — cleancode

Comment on lines +3 to +6

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add class documentation.

The class lacks proper documentation explaining its purpose and functionality.

+/**+ * BadDesign class demonstrating poor code quality patterns.+ * This class is used for educational purposes to show anti-patterns.+ */ class BadDesign {-    // No class doc comment — cleancode
📝 Committable suggestion

‼️IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class BadDesign
{
// No class doc comment — cleancode
/**
* BadDesignclass demonstrating poor code quality patterns.
* Thisclass is usedfor educational purposes to show anti-patterns.
*/
class BadDesign
{
🤖 Prompt for AI Agents
In bad.php around lines 3 to 6, the class BadDesign lacks a doc comment. Add aclear class-level doc comment above the class declaration that describes thepurpose and functionality of the BadDesign class to improve code clarity andmaintainability.

public function messyFunction($a, $b)
{
// No function doc comment — cleancode

Comment on lines +7 to +10

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add method documentation.

The method lacks proper documentation explaining its parameters, return value, and behavior.

+    /**+     * Performs complex calculations with nested loops and conditionals.+     *+     * @param int $a First parameter for conditional logic+     * @param int $b Second parameter for conditional logic+     * @return int The computed sum after all operations+     */     public function messyFunction($a, $b)     {-        // No function doc comment — cleancode
📝 Committable suggestion

‼️IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
publicfunction messyFunction($a,$b)
{
// No function doc comment — cleancode
/**
* Performs complex calculations with nested loopsand conditionals.
*
* @param int$a First parameter for conditional logic
* @param int$b Second parameter for conditional logic
* @return int The computed sum after all operations
*/
publicfunction messyFunction($a,$b)
{
🤖 Prompt for AI Agents
In bad.php around lines 7 to 10, the method messyFunction lacks a doc comment.Add a proper PHPDoc block above the method that describes its purpose, detailsthe parameters $a and $b including their expected types, and specifies thereturn type and what the method returns or does.

$unused = 123; // unused variable — cleancode

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove unused variable.

The variable$unused is declared but never used, creating dead code.

-        $unused = 123; // unused variable — cleancode
📝 Committable suggestion

‼️IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$unused = 123; // unused variable — cleancode
🧰 Tools
🪛 PHPMD (2.15.0)

11-11: Avoid unused local variables such as '$unused'. (Unused Code Rules)

(UnusedLocalVariable)

🤖 Prompt for AI Agents
In bad.php at line 11, the variable $unused is declared but never used,resulting in dead code. Remove the declaration of the $unused variable entirelyto clean up the code.


// Long method with nested loops — codesize + design
$sum = 0;
for ($i = 0; $i < 10; $i++) {
for ($j = 0; $j < 10; $j++) {
$sum += $i * $j;
}
}
Comment on lines +13 to +19

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Extract nested loop logic into a separate method.

This nested loop structure increases complexity and makes the method harder to understand and test.

-        // Long method with nested loops — codesize + design-        $sum = 0;-        for ($i = 0; $i < 10; $i++) {-            for ($j = 0; $j < 10; $j++) {-                $sum += $i * $j;-            }-        }+        $sum = $this->calculateNestedSum();

Add this private method to the class:

/** * Calculate sum using nested loops. * * @return int The calculated sum */privatefunctioncalculateNestedSum():int{$sum =0;for ($i =0;$i <10;$i++) {for ($j =0;$j <10;$j++) {$sum +=$i *$j;        }    }return$sum;}
🤖 Prompt for AI Agents
In bad.php around lines 13 to 19, the nested loops calculating the sum increasemethod complexity. Extract this nested loop logic into a new private methodnamed calculateNestedSum that returns the computed sum. Replace the originalnested loop code with a call to this new method to improve readability andtestability.


// Excessive method complexity — design
if ($a > 10) {
if ($b < 5) {
for ($k = 0; $k < 5; $k++) {
$sum += $k;
}
} else {
while ($b > 0) {
$sum -= $b;
$b--;
}
}
}
Comment on lines +21 to +33

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Reduce cyclomatic complexity by extracting conditional logic.

The nested conditional structure with multiple branches significantly increases the method's complexity, making it harder to understand and maintain.

-        // Excessive method complexity — design-        if ($a > 10) {-            if ($b < 5) {-                for ($k = 0; $k < 5; $k++) {-                    $sum += $k;-                }-            } else {-                while ($b > 0) {-                    $sum -= $b;-                    $b--;-                }-            }-        }+        $sum += $this->applyConditionalCalculation($a, $b);

Add this private method to the class:

/** * Apply conditional calculation based on parameters. * * @param int $a First parameter * @param int $b Second parameter * @return int The adjustment to be added to sum */privatefunctionapplyConditionalCalculation(int$a,int$b):int{if ($a <=10) {return0;    }if ($b <5) {return$this->calculateSequentialSum();    }return$this->calculateDecrementSum($b);}privatefunctioncalculateSequentialSum():int{$adjustment =0;for ($k =0;$k <5;$k++) {$adjustment +=$k;    }return$adjustment;}privatefunctioncalculateDecrementSum(int$b):int{$adjustment =0;while ($b >0) {$adjustment -=$b;$b--;    }return$adjustment;}
🤖 Prompt for AI Agents
In bad.php around lines 21 to 33, the nested conditional logic increasescyclomatic complexity. Refactor by extracting this logic into a new privatemethod named applyConditionalCalculation that takes $a and $b as parameters andreturns the adjustment to be added to $sum. Also create two helper privatemethods: calculateSequentialSum for the for-loop sum and calculateDecrementSumfor the while-loop decrement sum. Replace the original nested conditionals witha call to applyConditionalCalculation and add its return value to $sum.


return $sum;
}
Comment on lines +7 to +36

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider breaking down the method into smaller, focused methods.

ThemessyFunction method violates the Single Responsibility Principle by performing multiple unrelated operations. This makes it difficult to test, understand, and maintain.

Here's a refactored version following SOLID principles:

publicfunctionmessyFunction($a,$b):int{$sum =$this->calculateNestedSum();$sum +=$this->applyConditionalCalculation($a,$b);return$sum;}

This approach:

  • Improves readability and maintainability
  • Reduces cyclomatic complexity
  • Makes individual operations testable
  • Follows the Single Responsibility Principle
🧰 Tools
🪛 PHPMD (2.15.0)

11-11: Avoid unused local variables such as '$unused'. (Unused Code Rules)

(UnusedLocalVariable)

🤖 Prompt for AI Agents
In bad.php from lines 7 to 36, the messyFunction is too long and complex,violating the Single Responsibility Principle. Refactor by extracting the nestedloops calculation and the conditional logic into separate private methods, thencall these methods from messyFunction to compute and combine their results. Thiswill reduce complexity, improve readability, and make the code easier to testand maintain.

}

$obj = new BadDesign();
echo $obj->messyFunction(15, 3);

[8]ページ先頭

©2009-2025 Movatter.jp