Skip to content

Commit

Permalink
Adding phpstan to the CI process
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer committed Oct 3, 2024
1 parent 7b5ec17 commit 683319a
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ jobs:

- name: Run phpstan
run: bin/phpstan -V && bin/phpstan --error-format=github

- name: Run phpstan
run: bin/phpmd --version && composer phpmd
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@
"phpstan analyse src/"
],
"phpmd": [
"bin/phpmd ./src text cleancode,codesize,controversial,design"
"bin/phpmd ./src/ text phpmd.xml"
],
"benchmark": [
"bin/phpbench run tests/Benchmark/ --report=aggregate"
],
"all": [
"@cscheck",
"@analyze",
"@phpmd",
"@test"
],
"build-phar": [
Expand Down
21 changes: 21 additions & 0 deletions phpmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<ruleset name="My first PHPMD rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="
http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
Phauthentic PHPMD rule set
</description>

<!-- Import the entire unused code rule set -->
<rule ref="rulesets/cleancode.xml" />
<rule ref="rulesets/unusedcode.xml" />
<rule ref="rulesets/naming.xml">
<exclude name="LongVariable" />
</rule>
<rule ref="rulesets/codesize.xml" />

</ruleset>
14 changes: 5 additions & 9 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,9 @@ private function registerServices(): void
$this->containerBuilder->register(NodeTraverserInterface::class, NodeTraverser::class)
->setPublic(true);

if (getenv('APP_ENV') === 'test') {
$this->containerBuilder->register(OutputInterface::class, NullOutput::class)
->setPublic(true);
} else {
$this->containerBuilder->register(OutputInterface::class, ConsoleOutput::class)
->setPublic(true);
}
$outputClass = getenv('APP_ENV') === 'test' ? NullOutput::class : ConsoleOutput::class;
$this->containerBuilder->register(OutputInterface::class, $outputClass)
->setPublic(true);

$this->containerBuilder->register(InputInterface::class, ArgvInput::class)
->setPublic(true);
Expand Down Expand Up @@ -207,9 +203,9 @@ public function run(): void
);
}

public function get(string $id): mixed
public function get(string $identifier): mixed
{
return $this->containerBuilder->get($id);
return $this->containerBuilder->get($identifier);
}

public function getContainer(): ContainerBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/Business/Cognitive/BaselineService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function calculateDeltas(CognitiveMetricsCollection $metricsCollection, a
continue;
}

$previousMetrics = CognitiveMetrics::fromArray($methodData);
$previousMetrics = new CognitiveMetrics($methodData);
$metrics->calculateDeltas($previousMetrics);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Business/Cognitive/CognitiveMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use JsonSerializable;

/**
*
* @SuppressWarnings(PHPMD)
*/
class CognitiveMetrics implements JsonSerializable
{
Expand Down
2 changes: 1 addition & 1 deletion src/Business/Cognitive/CognitiveMetricsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private function isExcluded(string $classAndMethod): bool
$regexes = $this->configService->getConfig()->excludePatterns;

foreach ($regexes as $regex) {
if (preg_match('/' . $regex . '/', $classAndMethod, $matches)) {
if (preg_match('/' . $regex . '/', $classAndMethod)) {
return true;
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/Command/EventHandler/ProgressBarHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public function __construct(
public function __invoke(SourceFilesFound|FileProcessed $event): void
{
if ($event instanceof SourceFilesFound) {
foreach ($event->files as $file) {
$this->totalFiles++;
}

$this->totalFiles = count($event->files);
$this->output->writeln('Found ' . $this->totalFiles . ' files. Starting analysis.');
$this->progressBar = new ProgressBar($this->output, $this->totalFiles);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Command/EventHandler/VerboseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public function __invoke(SourceFilesFound|FileProcessed $event): void
private function formatBytes(int $size): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$i = 0;
$index = 0;

while ($size >= 1024 && $i < count($units) - 1) {
while ($size >= 1024 && $index < count($units) - 1) {
$size /= 1024;
$i++;
$index++;
}

return round($size, 2) . ' ' . $units[$i];
return round($size, 2) . ' ' . $units[$index];
}
}
6 changes: 6 additions & 0 deletions src/Config/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ConfigService
*/
private array $config;

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function __construct(
private readonly Processor $processor,
private readonly ConfigLoader $configuration
Expand All @@ -26,6 +29,9 @@ public function __construct(
]);
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function loadConfig(string $configFilePath): void
{
$this->config = $this->processor->processConfiguration($this->configuration, [
Expand Down

0 comments on commit 683319a

Please sign in to comment.