Skip to content

Commit c25b2e2

Browse files
committed
style: normalize code style with Rector
1 parent 959e5af commit c25b2e2

28 files changed

Lines changed: 95 additions & 69 deletions

.php-cs-fixer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
use PhpCsFixer\Finder;
4+
use PhpCsFixer\Config;
5+
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
6+
37
/**
48
* -------------------------------------------------------------------------
59
* Carbon plugin for GLPI
@@ -30,7 +34,7 @@
3034
* -------------------------------------------------------------------------
3135
*/
3236

33-
$finder = (new PhpCsFixer\Finder())
37+
$finder = (new Finder())
3438
->in(__DIR__)
3539
->exclude([
3640
'.git/',
@@ -41,9 +45,9 @@
4145
])
4246
;
4347

44-
return (new PhpCsFixer\Config())
48+
return (new Config())
4549
->setUnsupportedPhpVersionAllowed(true) // allow upcoming PHP versions
46-
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
50+
->setParallelConfig(ParallelConfigFactory::detect())
4751
->setCacheFile('files/_cache/php-cs-fixer/php-cs-fixer.cache')
4852
->setRules([
4953
'@PER-CS3.0' => true,

.twig_cs.dist.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
declare(strict_types=1);
44

5-
use FriendsOfTwig\Twigcs;
5+
use FriendsOfTwig\Twigcs\Finder\TemplateFinder;
6+
use FriendsOfTwig\Twigcs\Config\Config;
7+
use Glpi\Tools\GlpiTwigRuleset;
68

7-
$finder = Twigcs\Finder\TemplateFinder::create()
9+
$finder = TemplateFinder::create()
810
->in(__DIR__ . '/templates')
911
->depth('>= 0')
1012
->name('*.html.twig')
1113
->ignoreVCSIgnored(true);
1214

13-
return Twigcs\Config\Config::create()
15+
return Config::create()
1416
->setFinder($finder)
15-
->setRuleSet(\Glpi\Tools\GlpiTwigRuleset::class)
17+
->setRuleSet(GlpiTwigRuleset::class)
1618
;

install/Install.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace GlpiPlugin\Carbon;
3434

35+
use RuntimeException;
3536
use Config;
3637
use DBmysql;
3738
use DirectoryIterator;
@@ -112,7 +113,7 @@ public function install(array $args = []): bool
112113

113114
try {
114115
$DB->runFile($dbFile);
115-
} catch (\RuntimeException $e) {
116+
} catch (RuntimeException $e) {
116117
$this->migration->addWarningMessage("Error creating tables : " . $e->getMessage());
117118
return false;
118119
}
@@ -150,7 +151,7 @@ public function upgrade(string $from_version, array $args = []): bool
150151
$current_version = $matches[1];
151152
if (version_compare($db_version, $current_version) > 0) {
152153
// database more recent than current version
153-
$e = new \RuntimeException(sprintf(
154+
$e = new RuntimeException(sprintf(
154155
'Database of the plugin %s is more recent than the installed version %s.',
155156
$db_version,
156157
$current_version
@@ -167,19 +168,19 @@ public function upgrade(string $from_version, array $args = []): bool
167168
// Check the version os SEMVER compliant
168169
$regex = '!^' . self::SEMVER_REGEX . '$!';
169170
if (preg_match($regex, $this->force_upgrade_from_version) !== 1) {
170-
$e = new \RuntimeException('Invalid start version for upgrade.');
171+
$e = new RuntimeException('Invalid start version for upgrade.');
171172
trigger_error($e->getMessage(), E_USER_WARNING);
172173
throw $e;
173174
}
174175
if (version_compare($this->force_upgrade_from_version, $oldest_upgradable_version) < 0) {
175-
$e = new \RuntimeException('Upgrade is not supported before ' . $this->force_upgrade_from_version . '.');
176+
$e = new RuntimeException('Upgrade is not supported before ' . $this->force_upgrade_from_version . '.');
176177
trigger_error($e->getMessage(), E_USER_WARNING);
177178
throw $e;
178179
}
179180
}
180181
} else {
181182
if (version_compare($from_version, $oldest_upgradable_version, 'lt')) {
182-
$e = new \RuntimeException("Upgrade is not supported before $oldest_upgradable_version!");
183+
$e = new RuntimeException("Upgrade is not supported before $oldest_upgradable_version!");
183184
trigger_error($e->getMessage(), E_USER_WARNING);
184185
throw $e;
185186
}
@@ -288,7 +289,7 @@ public static function getOrCreateSource(string $name, int $fallback_level = 1,
288289
'name' => $name,
289290
]);
290291
if ($source->isNewItem()) {
291-
throw new \RuntimeException("Failed to create carbon intensity source '$name' in DB");
292+
throw new RuntimeException("Failed to create carbon intensity source '$name' in DB");
292293
}
293294
return $source->getID();
294295
}
@@ -310,7 +311,7 @@ public static function getOrCreateZone(string $name, int $source_id): int
310311
]);
311312
$zone->getFromDBByCrit(['name' => $name]);
312313
if ($zone->isNewItem()) {
313-
throw new \RuntimeException("Failed to create zone '$name' in DB");
314+
throw new RuntimeException("Failed to create zone '$name' in DB");
314315
}
315316
return $zone->getID();
316317
}

install/Uninstall.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace GlpiPlugin\Carbon;
3434

35+
use RuntimeException;
3536
use Config;
3637
use CronTask as GlpiCronTask;
3738
use DBmysql;
@@ -68,7 +69,7 @@ private function deleteConfig()
6869
{
6970
$config = new Config();
7071
if (!$config->deleteByCriteria(['context' => 'plugin:carbon'])) {
71-
throw new \RuntimeException('Error while deleting config');
72+
throw new RuntimeException('Error while deleting config');
7273
}
7374
}
7475

@@ -80,7 +81,7 @@ private function deleteRights()
8081
'name' => ['LIKE', 'carbon:%'],
8182
])
8283
) {
83-
throw new \RuntimeException('Error while deleting rights');
84+
throw new RuntimeException('Error while deleting rights');
8485
}
8586
}
8687

@@ -102,7 +103,7 @@ private function deleteDisplayPrefs()
102103
{
103104
$displayPreference = new DisplayPreference();
104105
if (!$displayPreference->deleteByCriteria(['itemtype' => CarbonIntensity::class])) {
105-
throw new \RuntimeException('Error while deleting display preferences');
106+
throw new RuntimeException('Error while deleting display preferences');
106107
}
107108
}
108109

src/AbstractImpact.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace GlpiPlugin\Carbon;
3434

35+
use LogicException;
3536
use CommonDBChild;
3637
use CommonDBTM;
3738
use DBmysql;
@@ -156,7 +157,7 @@ public static function getItemsToEvaluate(string $itemtype, array $crit = []): D
156157

157158
// Check $itemtype inherits from CommonDBTM
158159
if (!GlpiToolbox::isCommonDBTM($itemtype)) {
159-
throw new \LogicException('itemtype is not a CommonDBTM object');
160+
throw new LogicException('itemtype is not a CommonDBTM object');
160161
}
161162

162163
// clean $crit array: remove mostly SELECT, FROM

src/CarbonIntensity.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
namespace GlpiPlugin\Carbon;
3434

35+
use RuntimeException;
36+
use Exception;
3537
use CommonDropdown;
3638
use DateInterval;
3739
use DateTime;
@@ -318,7 +320,7 @@ public function save(string $zone_name, string $source_name, array $data): int
318320
'name' => $source_name,
319321
]);
320322
if ($source->isNewItem()) {
321-
throw new \RuntimeException('Attempt to save carbon intensity with a source which is not in the database');
323+
throw new RuntimeException('Attempt to save carbon intensity with a source which is not in the database');
322324
// trigger_error('Attempt to save carbon intensity with a source which is not in the database', E_USER_ERROR);
323325
// return 0;
324326
}
@@ -327,7 +329,7 @@ public function save(string $zone_name, string $source_name, array $data): int
327329
'name' => $zone_name,
328330
]);
329331
if ($zone->isNewItem()) {
330-
throw new \RuntimeException('Attempt to save carbon intensity with a zone which is not in the database');
332+
throw new RuntimeException('Attempt to save carbon intensity with a zone which is not in the database');
331333
// trigger_error('Attempt to save carbon intensity with a zone which is not in the database', E_USER_ERROR);
332334
// return 0;
333335
}
@@ -356,7 +358,7 @@ public function save(string $zone_name, string $source_name, array $data): int
356358
);
357359
$DB->executeStatement($stmt);
358360
$count++;
359-
} catch (\Exception $e) {
361+
} catch (Exception $e) {
360362
$count++;
361363
continue;
362364
}

src/CronTask.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace GlpiPlugin\Carbon;
3434

35+
use RuntimeException;
3536
use CommonDBTM;
3637
use CommonGLPI;
3738
use Config as GlpiConfig;
@@ -54,7 +55,7 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
5455
// Delegate to the client's crontask class the tab name to return
5556
// But keep here the logic to decide if a tab name shall be returned
5657
// to reduce class loading
57-
if (!is_a($item, GlpiCronTask::class)) {
58+
if (!$item instanceof GlpiCronTask) {
5859
return '';
5960
}
6061
if (!in_array($item->fields['itemtype'], CronTaskProvider::getCronTaskTypes())) {
@@ -66,7 +67,7 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
6667

6768
public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
6869
{
69-
if (is_a($item, GlpiCronTask::class)) {
70+
if ($item instanceof GlpiCronTask) {
7071
/** @var GlpiCronTask $item */
7172
$cron_task = new self();
7273
$cron_task->showForCronTask($item);
@@ -284,7 +285,7 @@ public static function downloadCarbonIntensityFromSource(GlpiCronTask $task, Cli
284285
$zone_name = $zone['name'];
285286
try {
286287
$added = $intensity->downloadOneZone($data_source, $zone_name, $limit_per_zone);
287-
} catch (\RuntimeException $e) {
288+
} catch (RuntimeException $e) {
288289
trigger_error($e->getMessage(), E_USER_WARNING);
289290
continue;
290291
}
@@ -342,7 +343,7 @@ public function fillIncompleteLocations(GlpiCronTask $task): int
342343
} catch (QuotaExceeded $e) {
343344
// If the quota is exceeded, stop the task
344345
break;
345-
} catch (\RuntimeException $e) {
346+
} catch (RuntimeException $e) {
346347
// If there is a runtime exception, log it and continue
347348
$failure = true;
348349
trigger_error($e->getMessage(), E_USER_WARNING);

src/Dashboard/Provider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace GlpiPlugin\Carbon\Dashboard;
3434

35+
use RuntimeException;
3536
use CommonDBTM;
3637
use Computer as GlpiComputer;
3738
use ComputerModel as GlpiComputerModel;
@@ -850,7 +851,7 @@ public static function getCarbonIntensity(array $params): array
850851
$count_request['COUNT'] = 'c';
851852
$count = $DB->request($count_request);
852853
if ($count->numrows() !== 1) {
853-
throw new \RuntimeException("Failed to count carbon intensity samples");
854+
throw new RuntimeException("Failed to count carbon intensity samples");
854855
}
855856
$date = CarbonIntensity::getTableField('date');
856857
$intensity = CarbonIntensity::getTableField('intensity');

src/DataSource/CarbonIntensity/AbortException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@
3232

3333
namespace GlpiPlugin\Carbon\DataSource\CarbonIntensity;
3434

35-
class AbortException extends \RuntimeException {}
35+
use RuntimeException;
36+
37+
class AbortException extends RuntimeException {}

src/DataSource/CarbonIntensity/AbstractClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace GlpiPlugin\Carbon\DataSource\CarbonIntensity;
3434

35+
use Generator;
3536
use Config as GlpiConfig;
3637
use DateInterval;
3738
use DateTime;
@@ -252,9 +253,9 @@ public function incrementalDownload(string $zone, DateTimeImmutable $start_date,
252253
*
253254
* @param DateTimeImmutable $start
254255
* @param DateTimeImmutable $stop
255-
* @return \Generator
256+
* @return Generator
256257
*/
257-
protected function sliceDateRangeByMonth(DateTimeImmutable $start, DateTimeImmutable $stop): \Generator
258+
protected function sliceDateRangeByMonth(DateTimeImmutable $start, DateTimeImmutable $stop): Generator
258259
{
259260
$real_start = $start->setTime((int) $start->format('H'), 0, 0, 0);
260261
$real_stop = $stop->setTime((int) $stop->format('H'), 0, 0, 0);
@@ -298,7 +299,7 @@ protected function sliceDateRangeByMonth(DateTimeImmutable $start, DateTimeImmut
298299
*
299300
* @param DateTimeImmutable $start
300301
* @param DateTimeImmutable $stop
301-
* @return \Generator
302+
* @return Generator
302303
*/
303304
protected function sliceDateRangeByDay(DateTimeImmutable $start, DateTimeImmutable $stop)
304305
{

0 commit comments

Comments
 (0)