Giter VIP home page Giter VIP logo

static-review's Introduction

static-review

This package is abandoned 🚨.

See GrumPHP for a maintained alternative.


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

Global Installation and Usage

The hooks can also be used for any project if you install static-review globally:

$ composer g require sjparkinson/static-review

Then, just install the hooks as you would normally but reference the global installation path:

$ static-review.php hook:install ~/.composer/vendor/sjparkinson/static-review/hooks/static-review-commit-msg.php .git/hooks/commit-msg

This assumes you have set up global composer paths.

Example Hooks

Static Review can be used for both files and commit message review. Below are basic hooks for each.

For Files

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

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

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

$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->files($git->getStagedFiles());

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

For Commit Messages

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

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

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

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

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

$git = new GitVersionControl();

// Review the current commit message.
// The hook is passed the file holding the commit message as the first argument.
$review->message($git->getCommitMessage($argv[1]));

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

Example Review For Files

class NoCommitTagReview extends AbstractFileReview
{
    // Review any text based file.
    public function canReviewFile(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, ReviewableInterface $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);
        }
    }
}

Example Review For Messages

class WorkInProgressReview extends AbstractMessageReview
{
    // Check if the commit message contains "wip"
    public function review(ReporterInterface $reporter, ReviewableInterface $commit)
    {
        $fulltext = $commit->getSubject() . PHP_EOL . $commit->getBody();

        if (preg_match('/\bwip\b/i', $fulltext)) {
            $message = 'Do not commit WIP to shared branches';
            $reporter->error($message, $this, $commit);
        }
    }
}

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
$ composer test

Licence

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

static-review's People

Contributors

adrian-martinez-interactiv4 avatar big-shark avatar john-n-smith avatar jpcercal avatar keradus avatar kingcrunch avatar shadowhand avatar sjparkinson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

static-review's Issues

Take account of the git staging area

I think you forget a tricky case: The reviewers will check the file in the current state on the fileSystem and not the file as it will be committed. It can occurred with a partial git add, but it will often append when you fix the violation and forget to add the file before the commit.
sample:

vim index.php
# edit file and add a violation
git add index.php
git commit # pre-commit reject the commit
vim index.php
# edit file to fix the violation
# notice: the modification is not added with "git add"
git commit # pre-commit do not reject the commit (file on the hard drive is OK but committed file is not)

To avoid that, you should not run reviewers directly on the original file, but on a copy of the snapshot of the file as it will be commited. You can retreive that snaphost with git shot :path/to/the/file Here is a bash version of a solution (not tested):

$TMP_DIR = /tmp/$(uuidgen)
mkdir -p $TMP_DIR
for FILE in $SFILES
do
    mkdir -p $TMP_DIR/$(dirname $FILE)
    git show :$FILE > $TMP_DIR/$FILE
done
./vendor/bin/phpcs --standard=PSR1 --encoding=utf-8 -n -p $TMP_DIR

rm -rf $TMP_DIR

Suggestion: add searchable topics to project

Now that GitHub has introduced its topic feature to make projects more searchable; I think it could really help others discover this great project, to add (at least) the following topics:

  • git-hooks
  • version-control
  • code-review
  • code-style
  • phpunit

These are just suggestions for you to take on board if you'd like...

Anyways thanks!

Better error messages for the command line tool

The command line tool could be improved with more checking for potential error cases and outputting more information.

Use of the ConsoleLogger I think would be a good refactor.

Also include better colouring ...

screen shot 2014-09-02 at 06 52 04 pm

... it currently gets a bit old, quick.

Php 7

Probably it will be good to remove allow failures php7 from travis matrix

ComposerLintReview Error

When I try to commit changes to a composer.json ComposerLintReview more often that not gives me the following error:

ComposerLintReview Error: The composer configuration is not valid in composer.json

If I manually run composer diagnose against the file it'll give me the following output (with exit code 0):

./composer.json is valid

Digging into the ComposerLintReview class and logging the output of the child process (if $process->isSuccessful() is false) gives me the following:

/var/folders/1_/j6pgbhz546g4t92sckw7215c0000gn/T/sjparkinson.static-review/cached/composer.json is valid

Digging deeper by manually running composer validate against the cache file above, I receive the following:

/var/folders/1_/j6pgbhz546g4t92sckw7215c0000gn/T/sjparkinson.static-review/cached/composer.json is valid
The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update`.

Not sure what's going here, but Composer is seemingly not a big fan of the static-review's cached composer file.

Any ideas?

Thanks!

CodeSniffer review doesn't handle command line errors

If there is a problem with your CodeSniffer set up (e.g. binary is not at vendor/bin/phpcs, your standard is not installed etc), you get little or not feedback as to the problem.

In the latter case, I got this error:

Warning: array_reduce() expects parameter 1 to be array, null given in vendor/sjparkinson/static-review/src/Review/PHP/PhpCodeSnifferReview.php on line 114
PHP Warning:  Invalid argument supplied for foreach() in vendor/sjparkinson/static-review/src/Review/PHP/PhpCodeSnifferReview.php on line 114

phpunit fail at windows at develop branch

phpunit fail at windows at develop branch:

PHPUnit 4.2.2 by Sebastian Bergmann.
...
There was 1 failure:

1) StaticReview\Test\Unit\Classes\FileTests::testGetMimeType
Failed asserting that false is true.

D:\GIT\fork\static-review\tests\unit\File\FileTest.php:125
C:\Users\keradus\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php:186
C:\Users\keradus\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php:138
�[37;41m                                        �[0m
�[37;41mFAILURES!                               �[0m
�[37;41mTests: 81, Assertions: 142, Failures: 1.�[0m

use global tools

Project recommend to install and then use some tools like phpcs etc.
It will be cool if project can use not only local (in a meaning of project) version of that tool, but as well global one, if there is one

phpunit fail at windows

After run phpunit at windows I get:
1) StaticReview\Test\Unit\Review\PHP\PhpLintReviewTest::testCanReview Failed asserting that false is true.
The reason is that PhpLintReview::canReview uses finfo_file to detect mime, which returns...
text/x-c++; charset=us-ascii

I known you prepare vagrant for testing, but I sb want to run tool on windows then it would be nice to make tests pass

Add phpunit/phpspec quality assurance review

Seeing that StaticReview can only review staged files and the Commit Message, what about preventing the commit if the testsuite is not passing? - I am doing this in a shell script via pre-commit and would really like to port it to static-review

Is this Package Abandoned?

@keradus asks in #68:

does this mean this package is abandoned?

Personally I'm not actively developing or using the project any more.

I was thinking about moving it to it's own org, static-review/static-review, and handing it over to the other contributors besides me, but that requires a rename. Not sure how much of a pain that would be.

Thoughts?

Incorrect EOL while getting changed files

Hello, i'm trying to run pre-commit script and getting error
failed to open stream: Invalid argument in \vendor\sjparkinson\static-review\src\File\File.php on line 174.

After code exploration i found the string that works wrong on my environment.
\vendor\sjparkinson\static-review\src\VersionControl\GitVersionControl.php:78
return array_filter(explode(PHP_EOL, $process->getOutput()));

I'm using window 7 + git bash + portable LAMP server (OpenServer)
In my case explode returns array that contains only one string.
But if i change PHP_EOL by "\n" it works.

Looks like php uses the system EOL while git bash console uses the linux one

Consider a global configuration file

I've had a look into the implementation details of the reviews. They have a lot of hardcoded configuration options (parameters) or even binary paths.
How about making them default options and overwritable by a simple YAML config in the project root dir?
Something like "php-static-review.yml" :-)

What do you think?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.