phpro / grumphp Goto Github PK
View Code? Open in Web Editor NEWA PHP code-quality tool
License: MIT License
A PHP code-quality tool
License: MIT License
Add phar file for simplicity
I'm not sure if it's grumphp error but just after installation ( composer require phpro/grumphp ) I get this error on each commit:
[Gitonomy\Git\Exception\ProcessException]
Error while running git:
error: Could not expand include path '~/.gitcinclude'
fatal: bad config file line 49 in /usr/local/git/etc/gitconfig
I've got grumphp 0.7.2 running on osx and my git config looks like this -> https://github.com/timcharper/git_osx_installer/blob/master/assets/etc/gitconfig.default
GrumPHP is not working on Windows CLI. This is because composer installs bat files for every bin file.
Some actions need to be taken:
Extra installation checks:
So I thought I'd give this a go after it was talked about at the the last Nomad Php Webinar I attended. I'm trying this on an empty project, the only code in my root that's not in my .gitignore is a readme.md, composer.json and grumphp.yml. On commit I get this error.
git commit
PHP Fatal error: Cannot redeclare composerRequire6fe528f671e512f2b4fb01800c0ca522() (previously declared in /Users/xxx/yyy/zzz/vendor/composer/autoload_real.php:52) in /Users/xxx/yyy/zzz/vendor/composer/autoload_real.php on line 55
Fatal error: Cannot redeclare composerRequire6fe528f671e512f2b4fb01800c0ca522() (previously declared in /Users/xxx/yyy/zzz/vendor/composer/autoload_real.php:52) in /Users/xxx/yyy/zzz/vendor/composer/autoload_real.php on line 55
As mentioned in #14, the Task::run() method can be very large if custom formatting is added.
Therefor a Formatter interface will be available. It will look something like this:
interface Formatter
{
public function formatErrorMessage($output);
public function formatSuccessMessage($output);
}
The implementation could also format success / error messages to a specific treshold.
After installing grumphp with composer, there needs to be a check if a grumphp.yml
file exists.
If it does not exist an extra command needs to be executed. This command will load default values from composer.json and will try to guess the git path.
The resulting file will be placed in the composer directory.
This command will run before the git hook is added. This way, we can always be sure that the config file is pointing to the correct locations.
Hey guys,
For some reason i can still put my blacklisted keywords in my commit messages:
This is my grumpphp file
parameters:
git_dir: .
bin_dir: ./vendor/bin
tasks:
# Analyzes some PHP source code and tries to fix coding standards issues
# Documentation for phpcsfixer: https://github.com/phpro/grumphp#php-cs-fixer
# and for the used library PHP-CS-Fixer: https://github.com/FriendsOfPHP/PHP-CS-Fixer#usage
phpcsfixer:
level: "psr2"
filters:
#You can also blacklist the fixers you don't want by placing a dash in front of the fixer name, if this is more convenient
- "-indentation"
- "-elseif"
- "-php_closing_tag"
# The Phpcs task will sniff your code for bad coding standards.
# Documentation for PHPCS https://github.com/phpro/grumphp#phpcs
# ----
# For now this task is commented because none of our theme classes are in namespaces and psr2 wants this.
# ----
phpcs:
standard: "PSR2"
# The Blacklist task will test your changes for blacklisted keywords in the commit messages
# Documetation for blacklist https://github.com/phpro/grumphp#blacklist
blacklist:
keywords:
- "die("
- "var_dump("
- "exit;"
- " "
- "global styling"
- "spacings"
- "test"
Here are some successfull commit messages:
commit 4828296a9b786fafbed7b0d9bbdfe4ba54ddf4c8
Author: Dennis Stolmeijer <*@hotmail.com>
Date: Mon Nov 16 00:26:47 2015 +0100
global styling
commit 3a754f42afdc78da4cd7dd0c35fa2ec19036e9c5
Author: Dennis Stolmeijer <*@hotmail.com>
Date: Mon Nov 16 00:20:59 2015 +0100
global styling
commit d185f76c52414fcdec01861d8b0e0b69421dd314
Author: Dennis Stolmeijer <*@hotmail.com>
Date: Mon Nov 16 00:19:58 2015 +0100
die()
commit d7cd48f4d94f106bb1000f84861b8afb24563d59
Author: Dennis Stolmeijer <*@hotmail.com>
Date: Mon Nov 16 00:19:16 2015 +0100
die()
Hope you guys can help me out
We are planning to replace current ASCII art with a mascot.
A happy one when the tasks pass, a grumpy one when the tasks fail.
Here are 2 possible mascots. Note that the second one is based on a copyrighted image. Not sure how far we need to alter the mascot to use it.
Mascot 2:
Original image: http://phil-crash-murphy.deviantart.com/art/Hey-I-m-Grump-366700350
Remove git pre-commit hook when uninstalling grumphp
Ensure windows compatibility through continuous integration:
https://www.appveyor.com/
Samle configuration:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/1.12/appveyor.yml
Create it if not found!
While I run tests on my future new task proposal for php_blacklist
commented in issue #53, I found an error in RegisteredFiles Locator.
See https://github.com/phpro/grumphp/blob/master/src/GrumPHP/Locator/RegisteredFiles.php#L35
PHP_EOL
must be replaced by "\n"
I tried to do a global install on a Windows 7 64-bit machine, but I am getting the error mentioned in the topic.
$ composer global require phpro/grumphp
Changed current directory to C:/Users/lasleh/AppData/Roaming/Composer
Using version ^0.5.1 for phpro/grumphp
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing gitonomy/gitlib (v0.1.7)
Downloading: 100%
- Installing doctrine/collections (v1.3.0)
Loading from cache
- Installing symfony/config (v2.7.6)
Loading from cache
- Installing symfony/dependency-injection (v2.7.6)
Loading from cache
- Installing symfony/yaml (v2.7.6)
Loading from cache
- Installing symfony/expression-language (v2.7.6)
Downloading: 100%
- Installing phpro/grumphp (v0.5.1)
Downloading: 100%
symfony/dependency-injection suggests installing symfony/proxy-manager-bridge (Generate service proxies to lazy load them)
phpro/grumphp suggests installing behat/behat (Lets GrumPHP validate your project features.)
phpro/grumphp suggests installing phpspec/phpspec (Lets GrumPHP spec your code.)
phpro/grumphp suggests installing phpunit/phpunit (Lets GrumPHP run your unit tests.)
phpro/grumphp suggests installing squizlabs/php_codesniffer (Lets GrumPHP sniff on your code.)
phpro/grumphp suggests installing roave/security-advisories (Lets GrumPHP be sure that there are no known security issues.) Writing lock file
Generating autoload files
Created git hooks folder at: ./.git/hooks/
Watch out! GrumPHP is sniffing your commits!
lasleh@CPB2265
$ grumphp
PHP Fatal error: Class 'GrumPHP\Console\Application' not found in C:\Users\lasleh\AppData\Roaming\Composer\vendor\phpro\grumphp\bin\grumphp on line 31
Fatal error: Class 'GrumPHP\Console\Application' not found in C:\Users\lasleh\AppData\Roaming\Composer\vendor\phpro\grumphp\bin\grumphp on line 31
In a Task service it must be possible to add a "blocking" tag to the service configuration.
E.g.:
parameters:
tasks:
taskname:
metadata:
blocking: false
By default all tasks are blocking. A user can overwrite the task service to be non blocking.
When a task is blocking, the errors will be fatal and shown in red.
When a task is not blocking, the error will not be fatal. A yellow error message will be displayed, but the changes will be commited.
The blocking flag of a task should be read when a tasks fails.
The metadata functionallity is already available with the getTaskMetadata()
method on the GrumPHP object.
More information: https://github.com/phpro/grumphp/blob/master/doc/tasks.md#metadata
Hello, thanks for your library.
I suggest add new feature - run hook by grumphp.phar
Advantages:
Currently all tasks will run, even if one task fails.
A parameter which makes it possible to fail early should be added..
For example:
parameters:
stop_on_failure: true
By default this value is false
.
In composer.json
all Symfony components require version 2.6 or above, but a lot of projects use Symfony 2.3 (like mine) because it is an LTS release. Is it possible to downgrade component versions to 2.3?
After removing GrumPHP with composer, the hook referring to the GrumPHP is still present. This prevents me from committing unless I remove the hook manually.
This can be annoying for people just trying out GrumPHP and they decide to remove it from their project.
Grumphp expects that the git root is in the same folder as the vendor folder.
Directory structure:
–– app
–––– .git
–––– .gitignore
–––– src
–––––– grumphp.yml
–––––– vendor
–––––––– phpro
–––––––––– grumphp
Hi guys.
That would be great if we can write some parameter at composer.json
file extras that will be used at grumphp
when it installed via composer install
or added via composer require
.
Something like this:
{
"extra": {
"grumphp": {
"config-default-path": "vendor/our/package/path/to/config.yml"
}
}
}
Lets discuss and I will implement this.
Thanks.
Hello!
You should change your implementation for detecting functions.
You may want to take a look at how I've done it in my package: https://github.com/nilportugues/php_forbidden_functions and bring it to your code or use it. Up to you :)
But blacklisting die(
will detect when die()
but also ->die()
I started building a similar library because I forgot this existed. I wanted to have a solution similar to Rocketeer which allowed people to create a directory in their project .githooks
which contained a php config file with the tasks they wanted to do with particular git commands. The idea being companies can create their own plugins to do particular tasks. post-commit
for example could post to a Slack channel, etc.
It would allow people to easily create their own tasks with classes or callbacks. For example I could have a task that did a test merge with the parent branch to check for an conflicts before I push.
I don't want to reinvent the wheel and release a new library where 80% of the work is achieved with GrumPHP. But to make GrumPHP work how I'd like I'd need to refactor a lot of the structure. But it would allow users to specify only the plugins they required and have it all managed through composer as well as other tasks which might not have a library around them.
Any thoughts?
A colleague at my current day job project suggested GrumPHP might be able to use (for example) Ant's build.xml as task configuration.
It seems interesting.
If I'm reading the code correctly, you can only use Git Blacklist with PHP files. It would be very useful to allow this to work with other files types as well.
E.g. Javascript files with console.log(…)
or debugger;
statements.
Now GrumPHP check the index/stage for files changed as desired, but then is check for the content of these files in the working directory, so, you can modify these files locally without adding them to the index (e.g. for commit them) and GrumPHP will pass.
I don't know if is easy or even possible to check for content of the files but in the git index.
It might be useful to use GrumPHP with CI servers like Jenkins.
An additional command needs to be created to run the tasks during Continious Integration.
Note that it should be possible to:
When the installation of grumphp is global, a wrong dependency could be use by grumphp
Given a global installation of grumphp
Given a project with symfony/console=2.1.0 dependency
When I am in the project directory
And I run:
"""
grumphp run -c grumphp.yml
"""
Then I get the error:
"""
PHP Fatal error: Call to undefined method Symfony\Component\Console\Helper\Helper::strlen() in /home/USER/.composer/vendor/symfony/console/Helper/ProgressBar.php on line 472
"""
bin/grumphp use the symfony/console=2.1.0 project dependency instead of global symfony/console dependency
At the moment, I do a hack in my global bin/grumphp to autoload only my global autoload
Hi.
After running command:
php ./vendor/bin/grumphp git:init --config=vendor/linkorb/convensions-checker/config/app/grumphp.yml
I have next .git/hooks/post-commit
:
#!/bin/sh
#
# Run the hook command.
# Note: this will be replaced by the real command during copy.
#
(cd "./" && exec 'vendor/bin/grumphp' 'git:pre-commit' '--config=../../../../../..') # <------------ MUST BE something like '--config=vendor/linkorb/convensions-checker/config/app/grumphp.yml'
...
What i'm doing wrong?
Thank you.
Hello, thanks a lot for your library.
I often commit changes in my vendor library, is it possible create pre-commit hook for vendor library?
Would it be possible to add a Codeception task? It's one of the major testing frameworks with PHPUnit and PHPSpec, would love to have it in Grumphp
During the composer task, warnings are triggered:
Running task 1/4: ComposerPHP Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
PHP Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
PHP Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
PHP Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
PHP Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
PHP Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
Warning: preg_match() expects parameter 2 to be string, object given in /Projects/phpro/grumphp/vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php on line 67
This does not occur during the run
command, but does during a git:pre-commit
command.
I think our README is becoming quite long and unwieldy to quickly navigate - I propose adding a separate folder for docs, where tasks can be documented separately and linking to those in the README.
I'll gladly do this if we're all agreed. :)
I'm currently working on a package within the vendor folder of a project. So, if I run grumphp, the following autoloaders are loaded:
This is causing errors when using the package beberlei/assert
in both the package and the project.
The following fixes the error, but I noticed the break
was removed in this commit (b23f63c).
// bin/grumphp
foreach ($autoloadLocations as $autoload) {
if (is_file($autoload)) {
require_once($autoload);
$loaded = true;
break;
}
}
Full output of the error:
$ php ./vendor/bin/grumphp run
PHP Fatal error: Cannot redeclare Assert\that() (previously declared in /var/www/[project]/vendor/[company]/[package]/vendor/beberlei/assert/lib/Assert/functions.php:36) in /var/www/[project]/vendor/beberlei/assert/lib/Assert/functions.php on line 39
PHP Stack trace:
PHP 1. {main}() /var/www/[project]/vendor/[company]/[package]/vendor/phpro/grumphp/bin/grumphp:0
PHP 2. require_once() /var/www/[project]/vendor/[company]/[package]/vendor/phpro/grumphp/bin/grumphp:16
PHP 3. ComposerAutoloaderInit6549f0c697930168ee67730da82614b8::getLoader() /var/www/[project]/vendor/autoload.php:7
PHP 4. composerRequire6549f0c697930168ee67730da82614b8($file = *uninitialized*) /var/www/[project]/vendor/composer/autoload_real.php:45
Fatal error: Cannot redeclare Assert\that() (previously declared in /var/www/[project]/vendor/[company]/[package]/vendor/beberlei/assert/lib/Assert/functions.php:36) in /var/www/[project]/vendor/beberlei/assert/lib/Assert/functions.php on line 39
Call Stack:
0.0006 228320 1. {main}() /var/www/[project]/vendor/[company]/[package]/vendor/phpro/grumphp/bin/grumphp:0
0.0083 586808 2. require_once('/var/www/[project]/vendor/autoload.php') /var/www/[project]/vendor/[company]/[package]/vendor/phpro/grumphp/bin/grumphp:16
0.0087 598408 3. ComposerAutoloaderInit6549f0c697930168ee67730da82614b8::getLoader() /var/www/[project]/vendor/autoload.php:7
0.0239 1199256 4. composerRequire6549f0c697930168ee67730da82614b8(???) /var/www/[project]/vendor/composer/autoload_real.php:45
Always fixed to 0.7.0
See https://github.com/phpro/grumphp/blob/master/src/GrumPHP/Console/Application.php#L21
For example:
grumphp run --skip=phpunit
grumphp run --skip=phpunit,behat
Currently the Task::run commands has many of these if statements:
if ($config['whatever']) {
$processBuilder->add();
}
It seems like a good idea to create a wrapper around the Symfony ProcessBuilder with following additional methods:
// Add an optional configuration option. This will not add anything if the config is empty
$wrapper->addOptionalAttribute('--config=%s', $config['config']);
// Add a required configuration options. Throws exception when the config is empty
$wrapper->addRequiredAttribute('--config=%s', $config['config']);
// Clone the processbuilder wrapper
$wrapper->clone()
Hello,
I have installed grumphp as a new project and started playing with one file. The structure is as follows:
src/MyBundle/MyTestClass.php
vendor/
.gitignore
grumphp.yml
composer.json
composer.lock
my grumphp.yml
parameters:
git_dir: .
bin_dir: ./vendor/bin
tasks:
blacklist:
keywords:
- "die("
- "var_dump("
- "exit;"
phpcs:
standard: "vendor/leaphub/phpcs-symfony2-standard/leaphub/phpcs/Symfony2/"
phpspec: ~
phpcsfixer: ~
I have added phpcs to composer as per your README page.
When I run grumphp command line for first time, it works very well and detect any issues related to symfony2 coding standards or blacklist keywords:
php ./vendor/bin/grumphp git:pre-commit --config=grumphp.yml
However if I make any change to MyTestClass.php file, it will not detect it after running the command again. Interestingly when I left the code for some time (around 1 hour) and ran it again it worked fine again! Also when I run phpcs directly from command line I am able to see if there is any issues with code so I suspect that there is caching or something that affects running tasks.
do you use any caching with grumphp?
my php code:
<?php
namespace File;
class MyTest_Class
{
protected $nameValue;
/**
* @return mixed
*/
public function getNameValue()
{
die();
exit;
return $this->nameValue;
}
/**
* @param string $nameValue
*
* @return mixed
*/
public function setNameValue($nameValue)
{
var_dump(null);
return $nameValue;
}
}
Hi. First of all - nice tool ! ;)
Maybe it's a matter of configuration, but please consider the following scenario:
git add foo.php
, git commit -m "foo"
-> fails due to PHP CS Fixer violationsgit status
will show unfixed-files in stage and fixed one in non-stage)git commit -m "foo"
will proceed, even if the file we are commiting is not validPlease, could you help?
When the test-suite grows, the tasks will run longer. Therefor it might be nice to display a progressbar while the tasks are running.
More information about the progressbar helper:
http://symfony.com/doc/current/components/console/helpers/progressbar.html
How should this work:
This feature will require some architecture changes which should be discussed before implementing.
Please, could you fix the name of tool showed on precommit hook?
Proper one is PHP CS Fixer
, or at least PhpCsFixer
if you don't like spaces.
First, thanks for your great work !
I faced a problem regarding PHP-CS-fixer and both pre-commit hook and run command.
When I exclude a file to be checked by cs-fixer through the .php_cs file, then execute the grumphp run
command ( the excluded file is tracked ), it fails because actually when we provide a file path in the php-cs-fixer bin, it overwrites the .php_cs config
I created an issue in the php-cs-fixer project in order to maybe change this behavior.
I'm stuck with the run command as my CI server rejects all my builds.
My question is, do you think we could execute php-cs-fixer task without file name passed as argument when we execute the run command?
Seems like there is some kind of problem with the relative paths in Git Bash and MINGW32 on Windows environments. It works if I place the absolute path to grumphp.yml, but I would rather not use that since it will break soon as somebody clones the repo to somewhere else. I tried various different options like "./" without help. I am using dev-master at the moment.
lasleh@CPB2265 /C
$ mkdir test
lasleh@CPB2265 /C
$ cd test
lasleh@CPB2265 /C/test
$ git init
Initialized empty Git repository in c:/test/.git/
lasleh@CPB2265 /C/test (master)
$ grumphp git:init
[Symfony\Component\Filesystem\Exception\IOException]
Failed to create "../../C:/test/.git/hooks/": mkdir(): Invalid argument.
Writing lock file
Generating autoload files
Warning: array_key_exists() expects parameter 2 to be array, null given in vendor/phpro/grumphp/src/GrumPHP/Configuration/TaskCompilerPass.php on line 27
In a Task service it must be possible to add a "priority" tag to the service configuration.
E.g.:
parameters:
tasks:
taskname:
metadata:
priority: 1000
The higher the priority, the earlier the task is executed.
The tasks will be sorted during runtime in the TasksCollection or in the TasksComilerPass.
The metadata functionallity is already available with the getTaskMetadata()
method on the GrumPHP object.
More information: https://github.com/phpro/grumphp/blob/master/doc/tasks.md#metadata
Hi.
When I run php ./vendor/bin/grumphp git:pre-commit
manually (and have no changed files) - I see green man even shouldn't. So, no tasks executed.
But tasks such as 'phpunit', 'phpspec', 'behat' need to be executed.
The success and error messages are way to big and distracting, displaying the massive logo every time, the pre-commit hook can already skip this output, but the commit-msg does not have this option.
Maybe it would be best to make this configurable in grumphp.yml?
In vagrant is succeeds in SourceTree it doesn't.
./bin/grumphp run
command in my vagrant-env it returns all good.GrumPHP detected a pre-commit command.
GrumPHP is sniffing your code!
Running task 1/1: Phpspec
/ skipped: 0% / pending: 0% / passed: 100% / failed: 0% / broken: 0% / 1 examples���������3 examples�����������30 examples�����������53 examples�����������99 examples�����������123 examples������������211 examples�����������52 examples�����������82 examples������������309 examples�����������96 examples������������406 examples�����������22 examples�����������41 examples�����������58 examples�����������67 examples�����������87 examples������������512 examples�����������43 examples�����������70 examples�����������98 examples������������627 examples
To skip commit checks, add -n or --no-verify flag to commit command
Completed with errors, see above
Because of this, I have to bypass the commit hooks.
I use the following environment:
OSX with PHP 5.5.29
Vagrant
SourceTree
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.