Skip to content

Commit

Permalink
correction of test files to have less issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dedece35 committed Jul 24, 2024
1 parent 1a7736a commit 0a37c7b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- upgrade docker system
- [#30](https://github.com/green-code-initiative/ecoCode-php/issues/30) Upgrade dependencies to latest ones - technical incompatibilities for SonarQube before 9.9 version
- clean unit test files (to have less other issues)

### Deleted

Expand Down
44 changes: 34 additions & 10 deletions src/test/resources/checks/AvoidDoubleQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,43 @@
$isStudent = true;
$cours = array('physique','chimie','informatique','philosophie');

echo $lastName;
echo $name;
echo '<br/>';
echo "<br/>"; // NOK {{Avoid using double quote ("), prefer using simple quote (')}}
echo $age;
mc = new MyClass();

mc->display($lastName);
mc->display($name);
mc->display('<br/>');
mc->display("<br/>"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}}
mc->display($age);

$identite = $lastName .' '. $name;
echo $identite;
mc->display($identite);

mc->myFunction($name, $age, $isStudent);

mc->myFunction("name", "age", "isStudent"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}}

mc->myFunction('name', 'age', 'isStudent');

myFunction($name, $age, $isStudent);
mc->myFunction("name", 'age', "isStudent"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}}

myFunction("name", "age", "isStudent"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}}

myFunction('name', 'age', 'isStudent');
class MyClass
{
public function myFunction($name, $age, $isStudent)
{
if ($name == null) {
return 'toto' + $age;
} else {
return 'tata' + $isStudent;
}
}

myFunction("name", 'age', "isStudent"); // NOK {{Avoid using double quote ("), prefer using simple quote (')}}
public function display($msg)
{
if ($msg != null) {
return $msg;
} else {
return '';
}
}
}
7 changes: 3 additions & 4 deletions src/test/resources/checks/AvoidFullSQLRequest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

$sql1 = 'SELECT * FROM'; // NOK {{Don't use the query SELECT * FROM}}
$sql2 = 'SeLeCt DiStInCt * FrOm'; // NOK {{Don't use the query SELECT * FROM}}
$sql3 = 'select name from';
$sql1 = 'SELECT * FROM toto'; // NOK {{Don't use the query SELECT * FROM}}
$sql2 = 'SeLeCt DiStInCt * FrOm tutu'; // NOK {{Don't use the query SELECT * FROM}}
$sql3 = 'select name from titi';

class AvoidFullSQLRequest
{
public function literalString()
{
OtherClass->SqlCall('SELECT * FROM'); // NOK {{Don't use the query SELECT * FROM}}
OtherClass->SqlCall('SeLeCt DiStInCt * FrOm'); // NOK {{Don't use the query SELECT * FROM}}
OtherClass->SqlCall('SeLeCt `table.name`, *, `my_field` FrOm'); // NOK {{Don't use the query SELECT * FROM}}
OtherClass->SqlCall('select name from');
}

Expand Down
14 changes: 7 additions & 7 deletions src/test/resources/checks/AvoidGettingSizeCollectionInLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* FOR STATEMENTS // RIGHT OPERAND
*/
for ($i = 0; $i < count($array); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
for ($i = 0; $i < count($array); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
var_dump($array[$i]);
}

Expand All @@ -14,7 +14,7 @@
var_dump($array[$i]);
}

for ($i = 0; $i < sizeof($array); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
for ($i = 0; $i < sizeof($array); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
var_dump($array[$i]);
}

Expand All @@ -23,7 +23,7 @@
var_dump($array[$i]);
}

for ($i = 0; $i < iterator_count(new ArrayIterator($array)); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
for ($i = 0; $i < iterator_count(new ArrayIterator($array)); ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
var_dump($array[$i]);
}

Expand All @@ -35,7 +35,7 @@
/**
* FOR STATEMENTS // LEFT OPERAND
*/
for ($i = 0; count($array) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
for ($i = 0; count($array) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
var_dump($array[$i]);
}

Expand All @@ -44,7 +44,7 @@
var_dump($array[$i]);
}

for ($i = 0; sizeof($array) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
for ($i = 0; sizeof($array) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
var_dump($array[$i]);
}

Expand All @@ -53,7 +53,7 @@
var_dump($array[$i]);
}

for ($i = 0; iterator_count(new ArrayIterator($array)) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
for ($i = 0; iterator_count(new ArrayIterator($array)) > $i; ++$i) { // NOK {{Avoid getting the size of the collection in the loop}}
var_dump($array[$i]);
}

Expand Down Expand Up @@ -115,7 +115,7 @@

$i = 0;
$size = count($array);
while ($size> $i) { // Compliant
while ($size > $i) { // Compliant
var_dump($array[$i]);
++$i;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/checks/AvoidSQLRequestInLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AvoidFullSQLRequest
private $dbName = 'name';
private $dbPass = 'pass';
private $dbHost = 'host';
private $query = 'SELECT * FROM Table';
private $query = 'SELECT col1 FROM Table';
private $otherQuery = 'SELECT name FROM User';
private $connection;
public function launchSQLRequest($someCondition)
Expand Down
16 changes: 4 additions & 12 deletions src/test/resources/checks/AvoidUsingGlobalVariablesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,19 @@

$a = 1;
$b = 2;

function somme() // NOK {{Prefer local variables to globals}}
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}

somme();
echo $b;

function somme2() // NOK {{Prefer local variables to globals}}
{
global $a, $b;
$b = $a + $b;
$c = $a + $b;
}

somme2();
echo $b;

function somme3($a, $b) // Compliant
function somme3($c, $d) // Compliant
{
return $a + $b;
$e = $c + $d;
return ++$e;
}

echo somme3($a, $b);
3 changes: 2 additions & 1 deletion src/test/resources/checks/IncrementCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
function foo()
{
$counter = 0;
return $counter++; // NOK {{Remove the usage of $i++. prefer ++$i}}
$counter++; // NOK {{Remove the usage of $i++. prefer ++$i}}
return $counter;
}

function bar()
Expand Down
18 changes: 9 additions & 9 deletions src/test/resources/checks/NoFunctionCallWhenDeclaringForLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

/* exemple 1 */

for ($i = 1; $i <= 10; $i++) {
for ($i = 1; $i <= 10; ++$i) {
echo $i;
}

/* exemple 2 */

for ($i = 1; ; $i++) {
for ($i = 1; ; ++$i) {
if ($i > 10) {
break;
}
Expand All @@ -18,30 +18,30 @@
/* exemple 3 */

$i = 1;
for (; ;) {
while (true) {
if ($i > 10) {
break;
}
echo $i;
$i++;
++$i;
}

/* exemple 4 */

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++); // NOK {{Do not call a function in for-type loop declaration}}
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, ++$i); // NOK {{Do not call a function in for-type loop declaration}}


function somewhat_calcMax()
function somewhatCalcMax()
{
return 500;
}


for ($i = 0; $i <= somewhat_calcMax(); $i++) { // NOK {{Do not call a function in for-type loop declaration}}
for ($i = 0; $i <= somewhatCalcMax(); ++$i) { // NOK {{Do not call a function in for-type loop declaration}}
var_dump($i);
}

$maxI = somewhat_calcMax();
for ($i = 0; $i <= $maxI; $i++) { // COMPLIANT
$maxI = somewhatCalcMax();
for ($i = 0; $i <= $maxI; ++$i) { // COMPLIANT
var_dump($i);
}

0 comments on commit 0a37c7b

Please sign in to comment.