Skip to content

Commit 68ce284

Browse files
authored
Add phpstan validation and fix suggested changes
1 parent 23b1ff2 commit 68ce284

15 files changed

+83
-21
lines changed

Jenkinsfile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ pipeline {
55
}
66

77
agent any
8+
9+
parameters {
10+
string(name: 'phpstan_level', defaultValue: '1', description: 'level used for phpstan validation.')
11+
}
812
stages {
913
stage('composer install') {
1014
steps {
@@ -20,6 +24,15 @@ pipeline {
2024
}
2125
}
2226
}
27+
28+
stage('phpstan tests') {
29+
steps {
30+
script {
31+
sh 'mkdir -p logs'
32+
sh 'vendor/bin/phpstan analyse -l ${phpstan_level} --error-format=junit > logs/phpstan_results.xml'
33+
}
34+
}
35+
}
2336
}
2437

2538
post {
@@ -63,4 +76,4 @@ pipeline {
6376
script: "echo ${GIT_COMMIT}|cut -c1-8"
6477
)
6578
}
66-
}
79+
}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ Release packages can be found on the [iTop Hub Store](https://store.itophub.io/e
1717
When downloading directly from GitHub (by cloning or downloading as zip) you will get potentially unstable code, and you will miss
1818
additional modules.
1919

20+
## Phpstan validation
21+
22+
run below command to validate your collector-base clone
23+
```composer install; vendor/bin/phpstan analyse```
24+
25+
see phpstan configuration in phpstan.neon.dist. in here you can increase/decrease (severity) level
26+
27+
to ignore remaining errors please generate baseline again:
28+
```composer install; vendor/bin/phpstan analyse --generate-baseline```
2029

2130
## About Us
2231

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"ext-libxml": "*"
55
},
66
"require-dev": {
7-
"phpunit/phpunit": "^9"
7+
"phpunit/phpunit": "^9",
8+
"phpstan/phpstan": "^2.1"
89
}
910
}

core/collectionplan.class.inc.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ public static function GetPlan()
5555
*/
5656
public function GetSortedLaunchSequence(): array
5757
{
58-
$aCollectorsLaunchSequence = utils::GetConfigurationValue('collectors_launch_sequence', []);
59-
$aExtensionsCollectorsLaunchSequence = utils::GetConfigurationValue('extensions_collectors_launch_sequence', []);
58+
$aCollectorsLaunchSequence = Utils::GetConfigurationValue('collectors_launch_sequence', []);
59+
$aExtensionsCollectorsLaunchSequence = Utils::GetConfigurationValue('extensions_collectors_launch_sequence', []);
6060
$aCollectorsLaunchSequence = array_merge($aCollectorsLaunchSequence, $aExtensionsCollectorsLaunchSequence);
61+
$aRank=[];
6162
if (!empty($aCollectorsLaunchSequence)) {
6263
// Sort sequence
6364
$aSortedCollectorsLaunchSequence = [];
@@ -150,4 +151,4 @@ public function AddCollectorsToOrchestrator(): bool
150151
return true;
151152
}
152153

153-
}
154+
}

core/collector.class.inc.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ public function InitSynchroDataSource($aPlaceholders)
501501
// Ok, found, is it up to date ?
502502
$aData = reset($aResult['objects']);
503503
$aCurrentSourceDefinition = $aData['fields'];
504+
$iKey=0;
504505
if (!array_key_exists('key', $aData)) {
505506
// Emulate the behavior for older versions of the API
506507
if (preg_match('/::([0-9]+)$/', $sKey, $aMatches)) {

core/csvcollector.class.inc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function Prepare()
7878
$this->aSynchroFieldsToDefaultValues = array();
7979
$this->bHasHeader = true;
8080

81+
$sCsvFilePath='';
8182
if (is_array($this->aCollectorConfig)) {
8283
if (array_key_exists('csv_file', $this->aCollectorConfig)) {
8384
$sCsvFilePath = $this->aCollectorConfig['csv_file'];
@@ -165,7 +166,7 @@ public function Prepare()
165166
Utils::Log(LOG_DEBUG, "[".get_class($this)."] Default values [".var_export($this->aSynchroFieldsToDefaultValues, true)."]");
166167

167168
if (!empty($this->sCsvCliCommand)) {
168-
utils::Exec($this->sCsvCliCommand);
169+
Utils::Exec($this->sCsvCliCommand);
169170
}
170171

171172
try {

core/jsoncollector.class.inc.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,21 @@ public function Prepare()
116116
}
117117
}
118118

119+
$aPath=[];
119120
if (isset($aParamsSourceJson["path"])) {
120121
$aPath = explode('/', $aParamsSourceJson["path"]);
121122
}
122123
if (isset($aParamsSourceJson["PATH"])) {
123124
$aPath = explode('/', $aParamsSourceJson["PATH"]);
124125
}
125-
if ($aPath == '') {
126+
if (count($aPath) == 0) {
126127
Utils::Log(LOG_ERR, "[".get_class($this)."] no path to find data in JSON file");
127128
}
128129

129130
//**** step 2 : get json file
130131
//execute cmd before get the json
131132
if (!empty($this->sJsonCliCommand)) {
132-
utils::Exec($this->sJsonCliCommand);
133+
Utils::Exec($this->sJsonCliCommand);
133134
}
134135

135136
//get Json file

core/sqlcollector.class.inc.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ abstract class SQLCollector extends Collector
3434
protected $oDB;
3535
protected $oStatement;
3636
protected $idx;
37+
protected $sQuery;
3738

3839
/**
3940
* Initalization
@@ -63,31 +64,31 @@ public function Prepare()
6364
}
6465

6566
// Read the SQL query from the configuration
66-
$sQuery = Utils::GetConfigurationValue(get_class($this)."_query", '');
67-
if ($sQuery == '') {
67+
$this->sQuery = Utils::GetConfigurationValue(get_class($this)."_query", '');
68+
if ($this->sQuery == '') {
6869
// Try all lowercase
69-
$sQuery = Utils::GetConfigurationValue(strtolower(get_class($this))."_query", '');
70+
$this->sQuery = Utils::GetConfigurationValue(strtolower(get_class($this))."_query", '');
7071
}
71-
if ($sQuery == '') {
72+
if ($this->sQuery == '') {
7273
// No query at all !!
7374
Utils::Log(LOG_ERR, "[".get_class($this)."] no SQL query configured! Cannot collect data. The query was expected to be configured as '".strtolower(get_class($this))."_query' in the configuration file.");
7475

7576
return false;
7677
}
7778

7879

79-
$this->oStatement = $this->oDB->prepare($sQuery);
80+
$this->oStatement = $this->oDB->prepare($this->sQuery);
8081
if ($this->oStatement === false) {
8182
$aInfo = $this->oDB->errorInfo();
82-
Utils::Log(LOG_ERR, "[".get_class($this)."] Failed to prepare the query: '$sQuery'. Reason: ".$aInfo[0].', '.$aInfo[2]);
83+
Utils::Log(LOG_ERR, "[".get_class($this)."] Failed to prepare the query: '$this->sQuery'. Reason: ".$aInfo[0].', '.$aInfo[2]);
8384

8485
return false;
8586
}
8687

8788
$this->oStatement->execute();
8889
if ($this->oStatement->errorCode() !== '00000') {
8990
$aInfo = $this->oStatement->errorInfo();
90-
Utils::Log(LOG_ERR, "[".get_class($this)."] Failed to execute the query: '$sQuery'. Reason: ".$aInfo[0].', '.$aInfo[2]);
91+
Utils::Log(LOG_ERR, "[".get_class($this)."] Failed to execute the query: '$this->sQuery'. Reason: ".$aInfo[0].', '.$aInfo[2]);
9192

9293
return false;
9394
}
@@ -223,15 +224,15 @@ protected function Connect()
223224
$this->oStatement = $this->oDB->prepare("SET NAMES 'utf8'");
224225
if ($this->oStatement === false) {
225226
$aInfo = $this->oDB->errorInfo();
226-
Utils::Log(LOG_ERR, "[".get_class($this)."] Failed to prepare the query: '$sQuery'. Reason: ".$aInfo[0].', '.$aInfo[2]);
227+
Utils::Log(LOG_ERR, "[".get_class($this)."] Failed to prepare the query: '$this->sQuery'. Reason: ".$aInfo[0].', '.$aInfo[2]);
227228

228229
return false;
229230
}
230231

231232
$bRet = $this->oStatement->execute();
232233
if ($this->oStatement->errorCode() !== '00000') {
233234
$aInfo = $this->oStatement->errorInfo();
234-
Utils::Log(LOG_ERR, "[".get_class($this)."] Failed to execute the query: '$sQuery'. Reason: ".$aInfo[0].', '.$aInfo[2]);
235+
Utils::Log(LOG_ERR, "[".get_class($this)."] Failed to execute the query: '$this->sQuery'. Reason: ".$aInfo[0].', '.$aInfo[2]);
235236

236237
return false;
237238
}

core/utils.class.inc.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ static public function Log($iPriority, $sMessage)
186186
case LOG_DEBUG:
187187
$sPrio = 'Debug';
188188
break;
189+
190+
default:
191+
$sPrio = 'Critical Error';
189192
}
190193

191194
if ($iPriority <= self::$iConsoleLogLevel) {
@@ -516,7 +519,7 @@ static public function DoPostRequest($sUrl, $aData, $sOptionnalHeaders = null, &
516519
}
517520
$response = @stream_get_contents($fp);
518521
if ($response === false) {
519-
throw new IOException("Problem reading data from $sUrl, $php_errormsg");
522+
throw new IOException("Problem reading data from $sUrl, " . error_get_last() );
520523
}
521524
if (is_array($aResponseHeaders)) {
522525
$aMeta = stream_get_meta_data($fp);
@@ -688,7 +691,9 @@ public static function CheckModuleInstallation(string $sModuleId, bool $bRequire
688691
{
689692
$oClient = new RestClient();
690693
}
691-
694+
695+
$sName = '';
696+
$sOperator = '';
692697
if (preg_match('/^([^\/]+)(?:\/([<>]?=?)(.+))?$/', $sModuleId, $aModuleMatches)) {
693698
$sName = $aModuleMatches[1];
694699
$sOperator = $aModuleMatches[2] ?? null ?: '>=';

phpstan-baseline.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Call to static method AddModule\(\) on an unknown class SetupWebPage\.$#'
5+
identifier: class.notFound
6+
count: 1
7+
path: test/getproject/module/module.myproject.php

0 commit comments

Comments
 (0)