-
Notifications
You must be signed in to change notification settings - Fork 3
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
PHPMD files #47
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,40 @@ | ||||||||||||||||||||||||||
<?php | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
class BadDesign | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// No class doc comment — cleancode | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
public function messyFunction($a, $b) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
// No function doc comment — cleancode | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Comment on lines
+7
to
+10
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. 🛠️ 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
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||
$unused = 123; // unused variable — cleancode | ||||||||||||||||||||||||||
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. Remove unused variable. The variable - $unused = 123; // unused variable — cleancode 📝 Committable suggestion
Suggested change
🧰 Tools🪛 PHPMD (2.15.0)11-11: Avoid unused local variables such as '$unused'. (Unused Code Rules) (UnusedLocalVariable) 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// 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
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. 🛠️ 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
*/
private function calculateNestedSum(): int
{
$sum = 0;
for ($i = 0; $i < 10; $i++) {
for ($j = 0; $j < 10; $j++) {
$sum += $i * $j;
}
}
return $sum;
} 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// 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
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. 🛠️ 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
*/
private function applyConditionalCalculation(int $a, int $b): int
{
if ($a <= 10) {
return 0;
}
if ($b < 5) {
return $this->calculateSequentialSum();
}
return $this->calculateDecrementSum($b);
}
private function calculateSequentialSum(): int
{
$adjustment = 0;
for ($k = 0; $k < 5; $k++) {
$adjustment += $k;
}
return $adjustment;
}
private function calculateDecrementSum(int $b): int
{
$adjustment = 0;
while ($b > 0) {
$adjustment -= $b;
$b--;
}
return $adjustment;
} 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return $sum; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Comment on lines
+7
to
+36
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. 🛠️ Refactor suggestion Consider breaking down the method into smaller, focused methods. The Here's a refactored version following SOLID principles: public function messyFunction($a, $b): int
{
$sum = $this->calculateNestedSum();
$sum += $this->applyConditionalCalculation($a, $b);
return $sum;
} This approach:
🧰 Tools🪛 PHPMD (2.15.0)11-11: Avoid unused local variables such as '$unused'. (Unused Code Rules) (UnusedLocalVariable) 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
$obj = new BadDesign(); | ||||||||||||||||||||||||||
echo $obj->messyFunction(15, 3); |
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.
🛠️ Refactor suggestion
Add class documentation.
The class lacks proper documentation explaining its purpose and functionality.
📝 Committable suggestion
🤖 Prompt for AI Agents