Skip to content

Commit

Permalink
refactor: cs, docblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitcukuren committed Aug 17, 2024
1 parent 3773e9e commit 9b9d8ad
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bin/lynter
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/Output/ColorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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
}
}
11 changes: 11 additions & 0 deletions src/Output/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 20 additions & 5 deletions tests/AnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@
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 = [
'rules' => [
'restrictFunction' => [
'functions' => ['eval'],
'message' => "Function '{name}' is not allowed.",
]
]
],
],
];

$ruleManager = new RuleManager($config);
Expand All @@ -25,18 +35,23 @@ 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 = [
'rules' => [
'restrictFunction' => [
'functions' => ['eval'],
'message' => "Function '{name}' is not allowed.",
]
]
],
],
];

$ruleManager = new RuleManager($config);
Expand Down
20 changes: 20 additions & 0 deletions tests/ConfigLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions tests/JsonFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
25 changes: 20 additions & 5 deletions tests/LynterCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
}
19 changes: 17 additions & 2 deletions tests/RawFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@
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();
$output = $formatter->format([]);
$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);
}
}
25 changes: 19 additions & 6 deletions tests/RuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,54 @@
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 = [
'rules' => [
'restrictFunction' => [
'functions' => ['eval'],
'message' => "Function '{name}' is not allowed.",
]
]
],
],
];

$ruleManager = new RuleManager($config);

$node = new FuncCall(new Name('eval'));
$issues = $ruleManager->applyRules($node);

$this->assertCount(1, $issues);
$this->assertSame("Function 'eval' is not allowed.", $issues[0]);
}

/**
* Tests applying rules when there are no issues.
*
* @return void
*/
public function testApplyRulesWithNoIssues(): void
{
$config = [
'rules' => [
'restrictFunction' => [
'functions' => ['eval'],
'message' => "Function '{name}' is not allowed.",
]
]
],
],
];

$ruleManager = new RuleManager($config);

$node = new FuncCall(new Name('someOtherFunction'));
$issues = $ruleManager->applyRules($node);

Expand Down

0 comments on commit 9b9d8ad

Please sign in to comment.