Skip to content

Commit

Permalink
fix cs
Browse files Browse the repository at this point in the history
  • Loading branch information
savinmikhail committed Jan 3, 2025
1 parent 9624f19 commit 27d416d
Show file tree
Hide file tree
Showing 37 changed files with 1,029 additions and 1,064 deletions.
6 changes: 3 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tasks:
cmd: 'vendor/bin/phpunit --coverage-html coverage {{.CLI_ARGS}}'

cs-fix:
cmd: 'vendor/bin/php-cs-fixer fix --diff --verbose --stop-on-violation {{.CLI_ARGS}}'
cmd: 'vendor/bin/php-cs-fixer fix --diff --verbose {{.CLI_ARGS}}'

cs:
cmds:
Expand Down Expand Up @@ -60,8 +60,8 @@ tasks:

check:
cmds:
- task: lint
- task: psalm
- task: cs
- task: stan
- task: rector
- task: deptrac-directories
- task: test
Expand Down
7 changes: 3 additions & 4 deletions src/Analyzer/AnalyzeFileTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function in_array;
use function is_array;
use function token_get_all;

use const T_COMMENT;
use const T_DOC_COMMENT;

Expand Down Expand Up @@ -48,9 +49,7 @@ public function run(SplFileInfo $file): array

$fileComments = $this->cache->get(
$cacheKey,
function () use ($filePath): iterable {
return $this->analyzeFile($filePath);
}
fn(): iterable => $this->analyzeFile($filePath),
);

$totalLinesOfCode = $this->countTotalLines($file->getRealPath());
Expand Down Expand Up @@ -93,7 +92,7 @@ private function shouldAnalyzeMissingDocBlocks(): bool
|| in_array(
$this->missingDocBlock->getName(),
$this->configDTO->getAllowedTypes(),
true
true,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Analyzer/CommentStatisticsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use SavinMikhail\CommentsDensity\Config\DTO\ConfigDTO;
use SavinMikhail\CommentsDensity\Exception\CommentsDensityException;
use SavinMikhail\CommentsDensity\MissingDocblock\MissingDocBlockAnalyzer;

use function substr_count;

use const PHP_EOL;

final readonly class CommentStatisticsAggregator
Expand Down
1 change: 1 addition & 0 deletions src/Baseline/Storage/TreePhpBaselineStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SavinMikhail\CommentsDensity\Baseline\Storage;

use SavinMikhail\CommentsDensity\Analyzer\DTO\Output\CommentDTO;

use const DIRECTORY_SEPARATOR;

final class TreePhpBaselineStorage implements BaselineStorageInterface
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace SavinMikhail\CommentsDensity\Commands;

use Generator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SavinMikhail\CommentsDensity\Config\ConfigLoader;
Expand Down
4 changes: 1 addition & 3 deletions src/Composer/CommentsDensityPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
use Composer\Script\Event;
use Composer\Script\ScriptEvents;

use function chmod;
use function copy;
use function file_exists;
use function file_put_contents;

final class CommentsDensityPlugin implements PluginInterface, EventSubscriberInterface
Expand Down Expand Up @@ -85,6 +82,7 @@ private static function promptForConfigFile(IOInterface $interface): void

if (!$shouldCreateConfig) {
$interface->write('Configuration file setup skipped.');

return;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Config/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use SavinMikhail\CommentsDensity\Config\DTO\ConfigDTO;
use SavinMikhail\CommentsDensity\Exception\CommentsDensityException;

use function defined;
use function dirname;
use function file_exists;

use const DIRECTORY_SEPARATOR;

final readonly class ConfigLoader
Expand Down
2 changes: 1 addition & 1 deletion src/Config/DTO/ConfigDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @param array<string, float> $thresholds
* @param string[] $exclude
* @param string[] $directories
* @param string[] $only
* @param string[] $disable
*/
public function __construct(
/** Limit occurrences of each comment type */
Expand Down
1 change: 1 addition & 0 deletions src/Reporters/ConsoleReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SavinMikhail\CommentsDensity\Analyzer\DTO\Output\PerformanceMetricsDTO;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;

use function array_map;

final readonly class ConsoleReporter implements ReporterInterface
Expand Down
2 changes: 2 additions & 0 deletions src/Reporters/HtmlReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace SavinMikhail\CommentsDensity\Reporters;

use SavinMikhail\CommentsDensity\Analyzer\DTO\Output\OutputDTO;

use function file_put_contents;
use function htmlspecialchars;
use function nl2br;

use const ENT_QUOTES;
use const ENT_SUBSTITUTE;

Expand Down
4 changes: 2 additions & 2 deletions tests/Analyzer/AnalyzerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public function testGetAnalyzer(): void
$analyzer = $factory->getAnalyzer($configDto, $output, $baselineStorage);

// Assert that the returned object is an instance of Analyzer
$this->assertInstanceOf(Analyzer::class, $analyzer);
self::assertInstanceOf(Analyzer::class, $analyzer);

// Use reflection to access the private property 'cache'
$reflection = new ReflectionClass($analyzer);
$cacheProperty = $reflection->getProperty('cache');
$cacheProperty->setAccessible(true);

// Assert that the Cache instance is set correctly in the Analyzer
$this->assertInstanceOf(Cache::class, $cacheProperty->getValue($analyzer));
self::assertInstanceOf(Cache::class, $cacheProperty->getValue($analyzer));
}
}
18 changes: 10 additions & 8 deletions tests/Analyzer/CommentStatisticsAggregatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
final class CommentStatisticsAggregatorTest extends TestCase
{
private MockObject $commentFactory;

private MockObject $missingDocBlock;

private CommentStatisticsAggregator $aggregator;

protected function setUp(): void
Expand All @@ -41,26 +43,26 @@ protected function setUp(): void
$this->aggregator = new CommentStatisticsAggregator(
$configDTO,
$this->commentFactory,
$this->missingDocBlock
$this->missingDocBlock,
);
}

public function testCalculateCommentStatisticsReturnsExpectedResult()
public function testCalculateCommentStatisticsReturnsExpectedResult(): void
{
$comments = [
new CommentDTO(
'missingDocblock',
'red',
'file.php',
2,
'Missing @throws tag'
'Missing @throws tag',
),
new CommentDTO(
'missingDocblock',
'red',
'file.php',
5,
'Missing @throws tag'
'Missing @throws tag',
),
new CommentDTO(
'regular',
Expand Down Expand Up @@ -91,18 +93,18 @@ public function testCalculateCommentStatisticsReturnsExpectedResult()

$result = $this->aggregator->calculateCommentStatistics($comments);

$this->assertEquals($expectedStatistics, $result);
self::assertEquals($expectedStatistics, $result);
}

public function testCalculateCommentStatisticsWithMissingDocBlock()
public function testCalculateCommentStatisticsWithMissingDocBlock(): void
{
$comments = [
new CommentDTO(
'missingDocblock',
'red',
'file.php',
2,
'Missing @throws tag'
'Missing @throws tag',
),
];

Expand All @@ -116,7 +118,7 @@ public function testCalculateCommentStatisticsWithMissingDocBlock()

$result = $this->aggregator->calculateCommentStatistics($comments);

$this->assertEquals($expectedStatistics, $result);
self::assertEquals($expectedStatistics, $result);
}

public function testCalculateCommentStatisticsThrowsExceptionForUnknownType(): void
Expand Down
43 changes: 23 additions & 20 deletions tests/Baseline/Storage/TreePhpBaselineStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use ReflectionClass;
use SavinMikhail\CommentsDensity\Analyzer\DTO\Output\CommentDTO;
use SavinMikhail\CommentsDensity\Baseline\Storage\TreePhpBaselineStorage;

use function ltrim;

use const DIRECTORY_SEPARATOR;

final class TreePhpBaselineStorageTest extends TestCase
{
private string $path;

private TreePhpBaselineStorage $storage;

protected function setUp(): void
Expand All @@ -34,7 +37,7 @@ public function testInitCreatesFileIfNotExists(): void
{
unlink($this->path);
$this->storage->init($this->path);
$this->assertFileExists($this->path);
self::assertFileExists($this->path);
}

public function testSetComments(): void
Expand All @@ -50,16 +53,16 @@ public function testSetComments(): void
'path' => [
'to' => [
'file1.php' => [
10 => ['comment' => 'Test comment 1', 'type' => 'regular']
10 => ['comment' => 'Test comment 1', 'type' => 'regular'],
],
'file2.php' => [
20 => ['comment' => 'Test comment 2', 'type' => 'regular']
]
]
]
20 => ['comment' => 'Test comment 2', 'type' => 'regular'],
],
],
],
];

$this->assertEquals($expectedData, include $this->path);
self::assertEquals($expectedData, include $this->path);
}

public function testFilterComments(): void
Expand All @@ -70,7 +73,7 @@ public function testFilterComments(): void
'red',
'/path/to/file1.php',
10,
'Test comment 1'
'Test comment 1',
),
];
$this->storage->setComments($existingComments);
Expand All @@ -83,10 +86,10 @@ public function testFilterComments(): void
$filteredComments = $this->storage->filterComments($comments);

$expectedFilteredComments = [
new CommentDTO('regular', 'red', '/path/to/file2.php', 20, 'Test comment 2')
new CommentDTO('regular', 'red', '/path/to/file2.php', 20, 'Test comment 2'),
];

$this->assertEquals($expectedFilteredComments, $filteredComments);
self::assertEquals($expectedFilteredComments, $filteredComments);
}

public function testAddCommentToTree(): void
Expand All @@ -104,13 +107,13 @@ public function testAddCommentToTree(): void
'path' => [
'to' => [
'file.php' => [
10 => ['comment' => 'test comment', 'type' => 'regular']
]
]
]
10 => ['comment' => 'test comment', 'type' => 'regular'],
],
],
],
];

$this->assertEquals($expectedTree, $tree);
self::assertEquals($expectedTree, $tree);
}

public function testCommentExistsInTree(): void
Expand All @@ -119,10 +122,10 @@ public function testCommentExistsInTree(): void
'path' => [
'to' => [
'file.php' => [
10 => ['comment' => 'Test comment', 'type' => 'regular']
]
]
]
10 => ['comment' => 'Test comment', 'type' => 'regular'],
],
],
],
];

$pathParts = explode(DIRECTORY_SEPARATOR, ltrim('/path/to/file.php', DIRECTORY_SEPARATOR));
Expand All @@ -133,6 +136,6 @@ public function testCommentExistsInTree(): void
$method->setAccessible(true);
$result = $method->invokeArgs($this->storage, [$tree, $pathParts, $line]);

$this->assertTrue($result);
self::assertTrue($result);
}
}
20 changes: 11 additions & 9 deletions tests/Cache/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace SavinMikhail\Tests\CommentsDensity\Cache;

use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SavinMikhail\CommentsDensity\Analyzer\DTO\Output\CommentDTO;
use SavinMikhail\CommentsDensity\Cache\Cache;

Expand All @@ -17,7 +19,7 @@ protected function setUp(): void
$this->cacheDir = __DIR__ . '/../../var/cache_tests/';
$this->clearCacheDir();
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 0777, true);
mkdir($this->cacheDir, 0o777, true);
}
}

Expand All @@ -39,8 +41,8 @@ public function testSetAndGetCache(): void
$cache->setCache($filePath, $comments);
$cachedData = $cache->getCache($filePath);

$this->assertNotNull($cachedData);
$this->assertEquals($comments, $cachedData);
self::assertNotNull($cachedData);
self::assertEquals($comments, $cachedData);

unlink($filePath);
}
Expand All @@ -57,25 +59,25 @@ public function testCacheInvalidationOnFileModification(): void

$cache->setCache($filePath, $data);
$cachedData = $cache->getCache($filePath);
$this->assertNotNull($cachedData);
$this->assertEquals($data, $cachedData);
self::assertNotNull($cachedData);
self::assertEquals($data, $cachedData);

// Modify the file to invalidate the cache
sleep(1); // Ensure the file modification time changes
file_put_contents($filePath, '<?php // modified content');

$cachedData = $cache->getCache($filePath);
$this->assertNull($cachedData);
self::assertNull($cachedData);

unlink($filePath);
}

private function clearCacheDir(): void
{
if (is_dir($this->cacheDir)) {
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->cacheDir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->cacheDir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);

foreach ($files as $fileinfo) {
Expand Down
Loading

0 comments on commit 27d416d

Please sign in to comment.