Skip to content

[Fix] Added padding to percentage string #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"roave/no-floaters": "^1.1",
"phpstan/phpstan": "^0.11.5",
"symfony/var-dumper": "^4.2",
"thecodingmachine/phpstan-strict-rules": "^0.11.0"
"thecodingmachine/phpstan-strict-rules": "^0.11.0",
"phpunit/phpunit": "^8.0",
"mockery/mockery": "^1.0"
},
"autoload-dev": {
"psr-4": {
Expand Down Expand Up @@ -58,14 +60,17 @@
"scripts": {
"ecs:test": "ecs check src --ansi --config vendor/symplify/easy-coding-standard/config/clean-code.yml",
"phpstan:test": "phpstan analyse --ansi",
"phpunit:test": "phpunit --colors=always",
"test": [
"@phpstan:test",
"@ecs:test"
"@ecs:test",
"@phpunit:test"
]
},
"scripts-descriptions": {
"ecs:test": "Run the ecs tests.",
"phpstan:test": "Run the phpstan tests.",
"test": "Run all tests including phpstan and ecs."
"phpunit:test": "Run the phpunit tests.",
"test": "Run all tests including phpstan, phpunit and ecs."
}
}
17 changes: 13 additions & 4 deletions src/Application/Console/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,16 @@ public function header(Results $results, string $dir): Style
$structureColor = "bg={$this->getColor($results->getStructure())}";
$styleColor = "bg={$this->getColor($results->getStyle())}";

$codeQuality = Style::getPercentageAsString($results->getCodeQuality());
$complexity = Style::getPercentageAsString($results->getComplexity());
$structure = Style::getPercentageAsString($results->getStructure());
$style = Style::getPercentageAsString($results->getStyle());

$output = <<<EOD
<$codeQualityColor> </> <$complexityColor> </> <$structureColor> </> <$styleColor> </>
<fg=black;options=bold;$codeQualityColor> {$this->getPercentageAsString($results->getCodeQuality())} </> <fg=black;options=bold;$complexityColor> {$this->getPercentageAsString($results->getComplexity())} </> <fg=black;options=bold;$structureColor> {$this->getPercentageAsString($results->getStructure())} </> <fg=black;options=bold;$styleColor> {$this->getPercentageAsString($results->getStyle())} </>
<fg=black;options=bold;$codeQualityColor> {$codeQuality} </> <fg=black;options=bold;$complexityColor>
{$complexity} </> <fg=black;options=bold;
$structureColor> {$structure} </> <fg=black;options=bold;$styleColor> {$style} </>
<$codeQualityColor> </> <$complexityColor> </> <$structureColor> </> <$styleColor> </>

<$subtitle>Code</> <$subtitle>Complexity</> <$subtitle>Architecture</> <$subtitle>Style</>
Expand Down Expand Up @@ -276,17 +283,19 @@ public function waitForKey(string $category): Style
}

/**
* Returns the percentage as 4 chars string.
* Returns the percentage as 5 chars string.
*
* @param float $percentage
*
* @return string
*/
private function getPercentageAsString(float $percentage): string
private static function getPercentageAsString(float $percentage): string
{
return sprintf('%s%%', $percentage === 100.0
$percentageString = sprintf('%s%%', $percentage === 100.0
? '100 '
: number_format($percentage, 1, '.', ''));

return str_pad($percentageString, 5);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Application/Console/StyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Tests\Application\Console;

use NunoMaduro\PhpInsights\Application\Console\Style;
use Tests\TestCase;

/**
* @covers \NunoMaduro\PhpInsights\Application\Console\Style
*/
final class StyleTest extends TestCase
{
/** @test */
public function stringHasCorrectLengthWhenOneDigitValue() : void
{
$percentageString = $this->invokeStaticMethod(Style::class, 'getPercentageAsString', 1);

$this->assertEquals(5, strlen($percentageString));
}

/** @test */
public function stringHasCorrectLengthWhenTWODigitValue() : void
{
$percentageString = $this->invokeStaticMethod(Style::class, 'getPercentageAsString', 10);

$this->assertEquals(5, strlen($percentageString));
}

/** @test */
public function stringHasCorrectLengthWhenThreeDigitValue() : void
{
$percentageString = $this->invokeStaticMethod(Style::class, 'getPercentageAsString', 100);

$this->assertEquals(5, strlen($percentageString));
}
}
31 changes: 31 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
use ReflectionClass;
use ReflectionException;

abstract class TestCase extends BaseTestCase
{
/**
* Call protected/private method of a class.
*
* @param string $class Instantiated object that we will run method on.
* @param string $methodName Method name to call.
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method result.
* @throws ReflectionException
*/
public function invokeStaticMethod(string $class, string $methodName, ...$parameters)
{
$reflection = new ReflectionClass($class);
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);

return $method->invokeArgs(null, $parameters);
}
}