Skip to content

Commit

Permalink
Add a job to generate churn.phar
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa committed Oct 23, 2020
1 parent b915a1b commit d7da76d
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
box.json.dist export-ignore
CHANGELOG.md export-ignore
churn.yml export-ignore
codor.xml export-ignore
Expand Down
54 changes: 54 additions & 0 deletions .github/workflows/build-phar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: "Phar construction"

on:
push:
paths-ignore:
- '**.md'
- 'img/**'
pull_request:
paths-ignore:
- '**.md'
- 'img/**'

jobs:
build:
name: "Build Phar"
runs-on: ubuntu-latest

steps:
- name: "Checkout"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "7.2"
tools: composer:v2

- name: "Install dependencies"
run: |
composer config platform.php 7.1.3
composer update --no-dev --no-interaction --no-progress --prefer-dist
composer config --unset platform.php
- name: "Download Box"
run: |
curl -sL https://github.com/box-project/box/releases/download/3.8.5/box.phar -o box.phar
chmod +x box.phar
- name: "Compile"
run: ./box.phar compile

- name: "Test Version"
run: diff <(bin/churn -V) <(./churn.phar -V)

- name: "Test Phar"
run: diff <(bin/churn run src) <(./churn.phar run src)

- name: "Save Phar"
uses: actions/upload-artifact@v2
with:
name: churn.phar
path: ./churn.phar
if-no-files-found: error
2 changes: 1 addition & 1 deletion .github/workflows/composer-normalize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

steps:
- name: "Checkout"
uses: "actions/checkout@v2.0.0"
uses: "actions/checkout@v2"

- name: "Run composer normalize"
uses: "docker://ergebnis/composer-normalize-action:latest"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "Continuous Integration"

on:
on:
push:
paths-ignore:
- '**.md'
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.lock
docs
vendor
coverage.xml
.idea
*.phar
.idea
2 changes: 2 additions & 0 deletions bin/churn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require_once(__DIR__ . '/bootstrap.php');

use Churn\Command\AssessComplexityCommand;
use Churn\Command\RunCommand;
use DI\ContainerBuilder;
use PackageVersions\Versions;
Expand All @@ -12,5 +13,6 @@ $container = ContainerBuilder::buildDevContainer();

$version = substr(Versions::getVersion('bmitch/churn-php'), 0, -33);
$application = new Application('churn-php', $version);
$application->add(new AssessComplexityCommand());
$application->add($container->get(RunCommand::class));
$application->run();
16 changes: 16 additions & 0 deletions box.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"dump-autoload": false,
"finder": [
{
"in": "bin"
},
{
"in": "src"
},
{
"name": "*.php",
"in": "vendor"
}
],
"output": "churn.phar"
}
37 changes: 37 additions & 0 deletions src/Command/AssessComplexityCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

namespace Churn\Command;

use Churn\Assessors\CyclomaticComplexity\CyclomaticComplexityAssessor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class AssessComplexityCommand extends Command
{
/**
* Configure the command
* @return void
*/
protected function configure(): void
{
$this->setName('assess-complexity')
->addArgument('file', InputArgument::REQUIRED, 'Path to file to analyze.')
->setDescription('Calculate the Cyclomatic Complexity');
}

/**
* Execute the command
* @param InputInterface $input Input.
* @param OutputInterface $output Output.
* @return integer
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$file = $input->getArgument('file');
$assessor = new CyclomaticComplexityAssessor();
$output->writeln($assessor->assess($file));
return 0;
}
}
13 changes: 10 additions & 3 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ private function getDirectoriesToScan(InputInterface $input, array $dirsConfigur
* @param ResultAccumulator $accumulator The object accumulating the results.
* @return OnSuccess
*/
private function getOnSuccessObserver(InputInterface $input, OutputInterface $output, ResultAccumulator $accumulator): OnSuccess
{
private function getOnSuccessObserver(
InputInterface $input,
OutputInterface $output,
ResultAccumulator $accumulator
): OnSuccess {
$observer = new OnSuccessAccumulate($accumulator);

if ((bool)$input->getOption('progress')) {
Expand Down Expand Up @@ -171,7 +174,11 @@ private function writeResult(InputInterface $input, OutputInterface $output, Res
$output->writeln("\n");
}
if (!empty($input->getOption('output'))) {
$output = new StreamOutput(fopen($input->getOption('output'), 'w+'), OutputInterface::VERBOSITY_NORMAL, false);
$output = new StreamOutput(
fopen($input->getOption('output'), 'w+'),
OutputInterface::VERBOSITY_NORMAL,
false
);
}

$renderer = $this->renderFactory->getRenderer($input->getOption('format'));
Expand Down
25 changes: 22 additions & 3 deletions src/Process/ProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Churn\Process;

use function array_merge;
use Churn\File\File;
use function getcwd;
use function is_callable;
use Phar;
use function strlen;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -47,10 +51,25 @@ public function createGitCommitProcess(File $file): ChurnProcess
*/
public function createCyclomaticComplexityProcess(File $file): ChurnProcess
{
$php = (new PhpExecutableFinder())->find();
$script = __DIR__ . '/../../bin/CyclomaticComplexityAssessorRunner';
$process = new Process([$php, $script, $file->getFullPath()]);
$command = array_merge(
[(new PhpExecutableFinder())->find()],
$this->getAssessorArguments(),
[$file->getFullPath()]
);
$process = new Process($command);

return new ChurnProcess($file, $process, 'CyclomaticComplexityProcess');
}

/**
* @return string[]
*/
private function getAssessorArguments(): array
{
if (is_callable([Phar::class, 'running']) && strlen(Phar::running(false)) > 0) {
return [Phar::running(false), 'assess-complexity'];
}

return [__DIR__ . '/../../bin/CyclomaticComplexityAssessorRunner'];
}
}
42 changes: 42 additions & 0 deletions tests/Integration/Command/AssessComplexityCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Churn\Tests\Integration\Command;

use Churn\Command\AssessComplexityCommand;
use Churn\Tests\BaseTestCase;
use function ctype_digit;
use DI\ContainerBuilder;
use function rtrim;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class AssessComplexityCommandTest extends BaseTestCase
{
/** @var CommandTester */
private $commandTester;

protected function setUp()
{
$container = ContainerBuilder::buildDevContainer();
$application = new Application('churn-php', 'test');
$application->add($container->get(AssessComplexityCommand::class));
$command = $application->find('assess-complexity');
$this->commandTester = new CommandTester($command);
}

protected function tearDown()
{
$this->commandTester = null;
}

/** @test */
public function it_returns_the_cyclomatic_complexity()
{
$exitCode = $this->commandTester->execute(['file' => __FILE__]);
$result = rtrim($this->commandTester->getDisplay());

$this->assertEquals(0, $exitCode);
$this->assertTrue(ctype_digit($result), 'The result of the command must be an integer');
$this->assertGreaterThan(0, (int) $result);
}
}

0 comments on commit d7da76d

Please sign in to comment.