From 9b9d8add41ef127ee29af94df3eee6cb4b34ad53 Mon Sep 17 00:00:00 2001 From: Yigit Cukuren Date: Sun, 18 Aug 2024 00:59:00 +0300 Subject: [PATCH] refactor: cs, docblocks --- bin/lynter | 2 +- src/Output/ColorHelper.php | 4 ++-- src/Output/JsonFormatter.php | 11 +++++++++++ tests/AnalyzerTest.php | 25 ++++++++++++++++++++----- tests/ConfigLoaderTest.php | 20 ++++++++++++++++++++ tests/JsonFormatterTest.php | 15 +++++++++++++++ tests/LynterCommandTest.php | 25 ++++++++++++++++++++----- tests/RawFormatterTest.php | 19 +++++++++++++++++-- tests/RuleManagerTest.php | 25 +++++++++++++++++++------ 9 files changed, 125 insertions(+), 21 deletions(-) diff --git a/bin/lynter b/bin/lynter index 0e8ab62..ad25211 100755 --- a/bin/lynter +++ b/bin/lynter @@ -11,7 +11,7 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php use Lynter\Command\LynterCommand; use Symfony\Component\Console\Application; -$application = new Application(); +$application = new Application('Lynter', '0.1.2'); // Register the LynterCommand $application->add(new LynterCommand()); diff --git a/src/Output/ColorHelper.php b/src/Output/ColorHelper.php index e3fa790..05607ca 100644 --- a/src/Output/ColorHelper.php +++ b/src/Output/ColorHelper.php @@ -45,7 +45,7 @@ public static function red(string $text): string */ public static function softRed(string $text): string { - return "\033[91m$text\033[0m"; // Softer Red (Bright Red) + return "\033[91m" . $text . self::COLOR_RESET; // Softer Red (Bright Red) } /** @@ -78,6 +78,6 @@ public static function cyan(string $text): string */ public static function gray(string $text): string { - return "\033[90m$text\033[0m"; // Gray + return "\033[90m" . $text . self::COLOR_RESET; // Gray } } diff --git a/src/Output/JsonFormatter.php b/src/Output/JsonFormatter.php index fb4dba2..dca5a55 100644 --- a/src/Output/JsonFormatter.php +++ b/src/Output/JsonFormatter.php @@ -2,8 +2,19 @@ namespace Lynter\Output; +/** + * Class JsonFormatter + * + * Formats analysis results as a JSON string. + */ class JsonFormatter implements FormatterInterface { + /** + * Format the analysis results as a JSON string. + * + * @param array $issues The array of issues found during analysis. + * @return string The formatted output as a JSON string. + */ public function format(array $issues): string { return json_encode($issues, JSON_PRETTY_PRINT); diff --git a/tests/AnalyzerTest.php b/tests/AnalyzerTest.php index fdbbbb6..7a76cde 100644 --- a/tests/AnalyzerTest.php +++ b/tests/AnalyzerTest.php @@ -6,8 +6,18 @@ use Lynter\RuleManager; use PHPUnit\Framework\TestCase; +/** + * Class AnalyzerTest + * + * Tests the functionality of the Analyzer class. + */ class AnalyzerTest extends TestCase { + /** + * Tests analyzing a file that contains issues. + * + * @return void + */ public function testAnalyzeFileWithIssues(): void { $config = [ @@ -15,8 +25,8 @@ public function testAnalyzeFileWithIssues(): void 'restrictFunction' => [ 'functions' => ['eval'], 'message' => "Function '{name}' is not allowed.", - ] - ] + ], + ], ]; $ruleManager = new RuleManager($config); @@ -25,9 +35,14 @@ public function testAnalyzeFileWithIssues(): void $issues = $analyzer->analyzeFile(__DIR__ . '/fixtures/with_issues.php'); $this->assertCount(1, $issues); - $this->assertSame('Function \'eval\' is not allowed.', $issues[0]['message']); + $this->assertSame("Function 'eval' is not allowed.", $issues[0]['message']); } + /** + * Tests analyzing a file that contains no issues. + * + * @return void + */ public function testAnalyzeFileWithNoIssues(): void { $config = [ @@ -35,8 +50,8 @@ public function testAnalyzeFileWithNoIssues(): void 'restrictFunction' => [ 'functions' => ['eval'], 'message' => "Function '{name}' is not allowed.", - ] - ] + ], + ], ]; $ruleManager = new RuleManager($config); diff --git a/tests/ConfigLoaderTest.php b/tests/ConfigLoaderTest.php index 55fffd7..432e37e 100644 --- a/tests/ConfigLoaderTest.php +++ b/tests/ConfigLoaderTest.php @@ -5,20 +5,40 @@ use Lynter\ConfigLoader; use PHPUnit\Framework\TestCase; +/** + * Class ConfigLoaderTest + * + * Tests the functionality of the ConfigLoader class. + */ class ConfigLoaderTest extends TestCase { + /** + * Tests loading a valid configuration file. + * + * @return void + */ public function testLoadValidConfig(): void { $config = ConfigLoader::load(__DIR__ . '/fixtures/valid_config.yml'); $this->assertArrayHasKey('rules', $config); } + /** + * Tests loading an invalid configuration file. + * + * @return void + */ public function testLoadInvalidConfig(): void { $this->expectException(\Exception::class); ConfigLoader::load(__DIR__ . '/fixtures/invalid_config.yml'); } + /** + * Tests loading a non-existent configuration file. + * + * @return void + */ public function testLoadNonExistentConfig(): void { $this->expectException(\Exception::class); diff --git a/tests/JsonFormatterTest.php b/tests/JsonFormatterTest.php index 9b86b55..ee73e9e 100644 --- a/tests/JsonFormatterTest.php +++ b/tests/JsonFormatterTest.php @@ -5,8 +5,18 @@ use Lynter\Output\JsonFormatter; use PHPUnit\Framework\TestCase; +/** + * Class JsonFormatterTest + * + * Tests the functionality of the JsonFormatter class. + */ class JsonFormatterTest extends TestCase { + /** + * Tests formatting with issues present. + * + * @return void + */ public function testFormatWithIssues(): void { $formatter = new JsonFormatter(); @@ -30,6 +40,11 @@ public function testFormatWithIssues(): void $this->assertJsonStringEqualsJsonString($expectedJson, $actualJson); } + /** + * Tests formatting with no issues present. + * + * @return void + */ public function testFormatWithoutIssues(): void { $formatter = new JsonFormatter(); diff --git a/tests/LynterCommandTest.php b/tests/LynterCommandTest.php index e094486..77130ad 100644 --- a/tests/LynterCommandTest.php +++ b/tests/LynterCommandTest.php @@ -7,9 +7,19 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; +/** + * Class LynterCommandTest + * + * Tests the functionality of the LynterCommand class. + */ class LynterCommandTest extends TestCase { - public function xtestExecuteWithNoIssues(): void + /** + * Tests the execution of the command when no issues are found. + * + * @return void + */ + public function testExecuteWithNoIssues(): void { $application = new Application(); $command = new LynterCommand(); @@ -25,6 +35,11 @@ public function xtestExecuteWithNoIssues(): void $this->assertStringContainsString('No issues found.', $output); } + /** + * Tests the execution of the command when issues are found. + * + * @return void + */ public function testExecuteWithIssues(): void { $application = new Application(); @@ -40,12 +55,12 @@ public function testExecuteWithIssues(): void $output = $commandTester->getDisplay(); // Check for the restricted 'eval' function - $this->assertStringContainsString('Function \'eval\' is not allowed.', $output); + $this->assertStringContainsString("Function 'eval' is not allowed.", $output); - // Check for another restricted function, e.g., 'exec' - $this->assertStringContainsString('Function \'shell_exec\' is not allowed.', $output); + // Check for another restricted function, e.g., 'shell_exec' + $this->assertStringContainsString("Function 'shell_exec' is not allowed.", $output); // Check for a non-restricted function to ensure it does not trigger an error - $this->assertStringNotContainsString('Function \'nonRestrictedFunction\' is not allowed.', $output); + $this->assertStringNotContainsString("Function 'nonRestrictedFunction' is not allowed.", $output); } } diff --git a/tests/RawFormatterTest.php b/tests/RawFormatterTest.php index 84aa93d..1971bbe 100644 --- a/tests/RawFormatterTest.php +++ b/tests/RawFormatterTest.php @@ -5,8 +5,18 @@ use Lynter\Output\RawFormatter; use PHPUnit\Framework\TestCase; +/** + * Class RawFormatterTest + * + * Tests the functionality of the RawFormatter class. + */ class RawFormatterTest extends TestCase { + /** + * Tests formatting output when no issues are found. + * + * @return void + */ public function testFormatWithNoIssues(): void { $formatter = new RawFormatter(); @@ -14,18 +24,23 @@ public function testFormatWithNoIssues(): void $this->assertStringContainsString('No issues found.', $output); } + /** + * Tests formatting output when issues are found. + * + * @return void + */ public function testFormatWithIssues(): void { $issues = [ [ 'file' => './test.php', 'line' => 10, - 'message' => 'Function \'eval\' is not allowed.' + 'message' => "Function 'eval' is not allowed." ] ]; $formatter = new RawFormatter(); $output = $formatter->format($issues); - $this->assertStringContainsString('Function \'eval\' is not allowed.', $output); + $this->assertStringContainsString("Function 'eval' is not allowed.", $output); } } diff --git a/tests/RuleManagerTest.php b/tests/RuleManagerTest.php index f89ae31..16d40f0 100644 --- a/tests/RuleManagerTest.php +++ b/tests/RuleManagerTest.php @@ -7,8 +7,18 @@ use PhpParser\Node\Name; use PHPUnit\Framework\TestCase; +/** + * Class RuleManagerTest + * + * Tests the functionality of the RuleManager class. + */ class RuleManagerTest extends TestCase { + /** + * Tests applying rules when a restricted function is used. + * + * @return void + */ public function testApplyRulesWithRestrictedFunction(): void { $config = [ @@ -16,12 +26,11 @@ public function testApplyRulesWithRestrictedFunction(): void 'restrictFunction' => [ 'functions' => ['eval'], 'message' => "Function '{name}' is not allowed.", - ] - ] + ], + ], ]; $ruleManager = new RuleManager($config); - $node = new FuncCall(new Name('eval')); $issues = $ruleManager->applyRules($node); @@ -29,6 +38,11 @@ public function testApplyRulesWithRestrictedFunction(): void $this->assertSame("Function 'eval' is not allowed.", $issues[0]); } + /** + * Tests applying rules when there are no issues. + * + * @return void + */ public function testApplyRulesWithNoIssues(): void { $config = [ @@ -36,12 +50,11 @@ public function testApplyRulesWithNoIssues(): void 'restrictFunction' => [ 'functions' => ['eval'], 'message' => "Function '{name}' is not allowed.", - ] - ] + ], + ], ]; $ruleManager = new RuleManager($config); - $node = new FuncCall(new Name('someOtherFunction')); $issues = $ruleManager->applyRules($node);