Skip to content

Commit

Permalink
Merging master
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer committed Oct 1, 2024
2 parents e5014ca + 565564e commit 33542c9
Show file tree
Hide file tree
Showing 56 changed files with 138 additions and 1,657 deletions.
2 changes: 1 addition & 1 deletion bin/cognitive-analysis
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ if (file_exists(__DIR__ . '/../../../autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}

use Phauthentic\CodeQualityMetrics\Application;
use Phauthentic\CognitiveCodeAnalysis\Application;

(new Application())->run();
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
},
"autoload": {
"psr-4": {
"Phauthentic\\CodeQualityMetrics\\": "src/"
"Phauthentic\\CognitiveCodeAnalysis\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Phauthentic\\CodeQualityMetrics\\Tests\\": "tests/"
"Phauthentic\\CognitiveCodeAnalysis\\Tests\\": "tests/"
}
},
"authors": [
Expand Down
8 changes: 0 additions & 8 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,3 @@ cognitive:
elseCount:
threshold: 1
scale: 1.0

halstead:
threshold:
difficulty: 0.0
effort: 0.0
time: 0.0
bugs: 0.0
volume: 0.0
9 changes: 0 additions & 9 deletions docs/Halstead-Complexity-Analysis.md

This file was deleted.

7 changes: 0 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ You can also pass a baseline file to compare the results to. The JSON report is
bin/cognitive-analysis metrics:cognitive <path-to-folder> --baseline cognitive.json
```

Halstead Complexity Analysis

```bash
bin/cognitive-analysis metrics:halstead <path-to-folder>
```

## Documentation

* [Cognitive Complexity Analysis](./docs/Cognitive-Complexity-Analysis.md#cognitive-complexity-analysis)
Expand All @@ -45,7 +39,6 @@ bin/cognitive-analysis metrics:halstead <path-to-folder>
* [Examples](#examples)
* [Wordpress WP_Debug_Data](#wordpress-wp_debug_data)
* [Doctrine Paginator](#doctrine-paginator)
* [Halstead Complexity Analysis](./docs/Halstead-Complexity-Analysis.md)

## Resources

Expand Down
64 changes: 19 additions & 45 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics;

use Phauthentic\CodeQualityMetrics\Business\Cognitive\BaselineService;
use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetricsCollector;
use Phauthentic\CodeQualityMetrics\Business\Cognitive\ScoreCalculator;
use Phauthentic\CodeQualityMetrics\Business\DirectoryScanner;
use Phauthentic\CodeQualityMetrics\Business\Halstead\HalsteadMetricsCollector;
use Phauthentic\CodeQualityMetrics\Command\Cognitive\CognitiveCollectorShellOutputPlugin;
use Phauthentic\CodeQualityMetrics\Command\CognitiveMetricsCommand;
use Phauthentic\CodeQualityMetrics\Command\HalsteadMetricsCommand;
use Phauthentic\CodeQualityMetrics\Business\MetricsFacade;
use Phauthentic\CodeQualityMetrics\Command\Presentation\CognitiveMetricTextRenderer;
use Phauthentic\CodeQualityMetrics\Command\Presentation\HalsteadMetricTextRenderer;
use Phauthentic\CodeQualityMetrics\Config\ConfigLoader;
use Phauthentic\CodeQualityMetrics\Config\ConfigService;
namespace Phauthentic\CognitiveCodeAnalysis;

use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\BaselineService;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollector;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\ScoreCalculator;
use Phauthentic\CognitiveCodeAnalysis\Business\DirectoryScanner;
use Phauthentic\CognitiveCodeAnalysis\Command\Cognitive\CognitiveCollectorShellOutputPlugin;
use Phauthentic\CognitiveCodeAnalysis\Command\CognitiveMetricsCommand;
use Phauthentic\CognitiveCodeAnalysis\Business\MetricsFacade;
use Phauthentic\CognitiveCodeAnalysis\Command\Presentation\CognitiveMetricTextRenderer;
use Phauthentic\CognitiveCodeAnalysis\Config\ConfigLoader;
use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService;
use PhpParser\NodeTraverser;
use PhpParser\NodeTraverserInterface;
use PhpParser\ParserFactory;
Expand Down Expand Up @@ -44,9 +41,6 @@ public function __construct()

private function registerServices(): void
{
$this->containerBuilder->register(HalsteadMetricsCollector::class, HalsteadMetricsCollector::class)
->setPublic(true);

$this->containerBuilder->register(CognitiveMetricsCollector::class, CognitiveMetricsCollector::class)
->setPublic(true);

Expand All @@ -56,9 +50,6 @@ private function registerServices(): void
$this->containerBuilder->register(ConfigService::class, ConfigService::class)
->setPublic(true);

$this->containerBuilder->register(HalsteadMetricTextRenderer::class, HalsteadMetricTextRenderer::class)
->setPublic(true);

$this->containerBuilder->register(CognitiveMetricTextRenderer::class, CognitiveMetricTextRenderer::class)
->setPublic(true);

Expand Down Expand Up @@ -100,14 +91,14 @@ private function registerServices(): void
private function bootstrap(): void
{
$this->registerServices();
$this->registerMetricsCollectors();
$this->registerConfigService();
$this->bootstrapMetricsCollectors();
$this->configureConfigService();
$this->registerMetricsFacade();
$this->registerCommands();
$this->registerApplication();
$this->configureApplication();
}

private function registerMetricsCollectors(): void
private function bootstrapMetricsCollectors(): void
{
$this->containerBuilder->register(CognitiveMetricsCollector::class, CognitiveMetricsCollector::class)
->setArguments([
Expand All @@ -120,17 +111,9 @@ private function registerMetricsCollectors(): void
]
])
->setPublic(true);

$this->containerBuilder->register(HalsteadMetricsCollector::class, HalsteadMetricsCollector::class)
->setArguments([
new Reference(ParserFactory::class),
new Reference(NodeTraverserInterface::class),
new Reference(DirectoryScanner::class),
])
->setPublic(true);
}

private function registerConfigService(): void
private function configureConfigService(): void
{
$this->containerBuilder->register(ConfigService::class, ConfigService::class)
->setArguments([
Expand All @@ -144,7 +127,6 @@ private function registerMetricsFacade(): void
{
$this->containerBuilder->register(MetricsFacade::class, MetricsFacade::class)
->setArguments([
new Reference(HalsteadMetricsCollector::class),
new Reference(CognitiveMetricsCollector::class),
new Reference(ScoreCalculator::class),
new Reference(ConfigService::class),
Expand All @@ -161,21 +143,13 @@ private function registerCommands(): void
new Reference(BaselineService::class),
])
->setPublic(true);

$this->containerBuilder->register(HalsteadMetricsCommand::class, HalsteadMetricsCommand::class)
->setArguments([
new Reference(MetricsFacade::class),
new Reference(HalsteadMetricTextRenderer::class),
])
->setPublic(true);
}

private function registerApplication(): void
private function configureApplication(): void
{
$this->containerBuilder->register(SymfonyApplication::class, SymfonyApplication::class)
->setPublic(true)
->addMethodCall('add', [new Reference(CognitiveMetricsCommand::class)])
->addMethodCall('add', [new Reference(HalsteadMetricsCommand::class)]);
->addMethodCall('add', [new Reference(CognitiveMetricsCommand::class)]);
}

public function run(): void
Expand Down
6 changes: 3 additions & 3 deletions src/Business/Cognitive/BaselineService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive;

use JsonException;
use RuntimeException;

/**
Expand All @@ -30,13 +31,12 @@ public function calculateDeltas(CognitiveMetricsCollection $metricsCollection, a
}
}


/**
* Loads the baseline file and returns the data as an array.
*
* @param string $baselineFile
* @return array<string, array<string, mixed>> $baseline
* @throws \JsonException
* @throws JsonException
*/
public function loadBaseline(string $baselineFile): array
{
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 @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive;

use InvalidArgumentException;
use JsonSerializable;
Expand Down
2 changes: 1 addition & 1 deletion src/Business/Cognitive/CognitiveMetricsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive;

use ArrayIterator;
use Closure;
Expand Down
41 changes: 25 additions & 16 deletions src/Business/Cognitive/CognitiveMetricsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive;

use Phauthentic\CodeQualityMetrics\Business\DirectoryScanner;
use Phauthentic\CodeQualityMetrics\CognitiveAnalysisException;
use Phauthentic\CodeQualityMetrics\Config\ConfigService;
use Phauthentic\CodeQualityMetrics\PhpParser\CognitiveMetricsVisitor;
use Phauthentic\CognitiveCodeAnalysis\Business\DirectoryScanner;
use Phauthentic\CognitiveCodeAnalysis\CognitiveAnalysisException;
use Phauthentic\CognitiveCodeAnalysis\Config\ConfigService;
use Phauthentic\CognitiveCodeAnalysis\PhpParser\CognitiveMetricsVisitor;
use PhpParser\Error;
use PhpParser\NodeTraverserInterface;
use PhpParser\Parser;
Expand All @@ -34,17 +34,29 @@ public function __construct(
$this->parser = $parserFactory->createForHostVersion();
}

/**
* @param array<string, mixed> $config
* @return array<int, string>
*/
private function getExcludePatternsFromConfig(array $config): array
{
if (isset($config['excludePatterns'])) {
return $config['excludePatterns'];
}

return [];
}

/**
* Collect cognitive metrics from the given path
*
* @param string $path
* @param array<string, mixed> $config
* @return CognitiveMetricsCollection
* @throws CognitiveAnalysisException
*/
public function collect(string $path, array $config = []): CognitiveMetricsCollection
{
$files = $this->findSourceFiles($path);
$files = $this->findSourceFiles($path, $this->getExcludePatternsFromConfig($config));

return $this->findMetrics($files);
}
Expand All @@ -54,9 +66,8 @@ public function collect(string $path, array $config = []): CognitiveMetricsColle
*
* @param iterable<SplFileInfo> $files
* @return CognitiveMetricsCollection
* @throws CognitiveAnalysisException
*/
protected function findMetrics(iterable $files): CognitiveMetricsCollection
private function findMetrics(iterable $files): CognitiveMetricsCollection
{
$metricsCollection = new CognitiveMetricsCollection();
$visitor = new CognitiveMetricsVisitor();
Expand Down Expand Up @@ -126,7 +137,7 @@ private function processMethodMetrics(
}
}

public function isExcluded(string $classAndMethod): bool
private function isExcluded(string $classAndMethod): bool
{
$regexes = $this->configService->getConfig()['cognitive']['excludePatterns'];

Expand All @@ -143,20 +154,18 @@ public function isExcluded(string $classAndMethod): bool
* Find source files using DirectoryScanner
*
* @param string $path Path to the directory or file to scan
* @param array<int, string> $exclude List of regx to exclude
* @return iterable<mixed, SplFileInfo> An iterable of SplFileInfo objects
*/
protected function findSourceFiles(string $path): iterable
private function findSourceFiles(string $path, array $exclude = []): iterable
{
return $this->directoryScanner->scan(
[$path],
['^(?!.*\.php$).+'] + $this->configService->getConfig()['cognitive']['excludeFilePatterns']
);
return $this->directoryScanner->scan([$path], ['^(?!.*\.php$).+'] + $exclude); // Exclude non-PHP files
}

/**
* @throws CognitiveAnalysisException
*/
protected function traverseAbstractSyntaxTree(string $code): void
private function traverseAbstractSyntaxTree(string $code): void
{
try {
$ast = $this->parser->parse($code);
Expand Down
2 changes: 1 addition & 1 deletion src/Business/Cognitive/Delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive;

/**
*
Expand Down
4 changes: 2 additions & 2 deletions src/Business/Cognitive/Exporter/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive\Exporter;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Exporter;

use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetricsCollection;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollection;
use RuntimeException;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Business/Cognitive/Exporter/DataExporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive\Exporter;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Exporter;

use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetricsCollection;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollection;

/**
*
Expand Down
4 changes: 2 additions & 2 deletions src/Business/Cognitive/Exporter/HtmlExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive\Exporter;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Exporter;

use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetricsCollection;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollection;
use RuntimeException;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Business/Cognitive/Exporter/JsonExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive\Exporter;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\Exporter;

use Phauthentic\CodeQualityMetrics\Business\Cognitive\CognitiveMetricsCollection;
use Phauthentic\CognitiveCodeAnalysis\Business\Cognitive\CognitiveMetricsCollection;
use RuntimeException;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Business/Cognitive/FindMetricsPluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Phauthentic\CodeQualityMetrics\Business\Cognitive;
namespace Phauthentic\CognitiveCodeAnalysis\Business\Cognitive;

use SplFileInfo;

Expand Down
Loading

0 comments on commit 33542c9

Please sign in to comment.