Skip to content

✋ An extendible framework for version control hooks.

License

Notifications You must be signed in to change notification settings

ChristopherWeldon/static-review

 
 

Repository files navigation

Static-Review

Latest Stable Version Build Status Minimum PHP Version

An extendable framework for version control hooks.

StaticReview Success Demo

Usage

For a composer managed project you can simply run the following ...

$ composer require sjparkinson/static-review

Hooks can then be installed like so ...

$ vendor/bin/static-review.php hook:install vendor/sjparkinson/static-review/hooks/example-pre-commit.php .git/hooks/pre-commit

Otherwise, if you don't use composer ...

$ git clone https://github.com/sjparkinson/static-review.git
$ cd static-review/
$ composer install --no-dev --optimize-autoloader
$ bin/static-review.php hook:install hooks/example-pre-commit.php ~/.../.git/hooks/pre-commit

Example Hook

Below is a basic hook that you can extend upon.

#!/usr/bin/env php
<?php

include __DIR__ . '/../../../autoload.php';

// Reference the required classes.
use StaticReview\StaticReview;
[...]

$reporter = new Reporter();
$review   = new StaticReview($reporter);

// Add any reviews to the StaticReview instance, supports a fluent interface.
$review->addReview(new LineEndingsReview());

$git = new GitVersionControl();

// Review the staged files.
$review->review($git->getStagedFiles());

// Check if any issues were found.
// Exit with a non-zero status to block the commit.
($reporter->hasIssues()) ? exit(1) : exit(0);

Example Review

class NoCommitTagReview extends AbstractReview
{
    // Review any text based file.
    public function canReview(FileInterface $file)
    {
        $mime = $file->getMimeType();

        // check to see if the mime-type starts with 'text'
        return (substr($mime, 0, 4) === 'text');
    }

    // Checks if the file contains `NOCOMMIT`.
    public function review(ReporterInterface $reporter, FileInterface $file)
    {
        $cmd = sprintf('grep --fixed-strings --ignore-case --quiet "NOCOMMIT" %s', $file->getFullPath());

        $process = $this->getProcess($cmd);
        $process->run();

        if ($process->isSuccessful()) {
            $message = 'A NOCOMMIT tag was found';
            $reporter->error($message, $this, $file);
        }
    }
}

Unit Tests

See vagrantup.com and phpunit.de.

$ git clone https://github.com/sjparkinson/static-review.git
$ cd static-review/
$ vagrant up
$ vagrant ssh
...
$ cd /srv
$ composer update
$ vendor/bin/phpunit
$ vendor/bin/phpcs --standard=PSR2 src/ bin/ hooks/ tests/

Licence

The content of this library is released under the MIT License by Samuel Parkinson.

About

✋ An extendible framework for version control hooks.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%