Coder Social home page Coder Social logo

stevegrunwell / wp-enforcer Goto Github PK

View Code? Open in Web Editor NEW
112.0 6.0 15.0 55 KB

Git hooks to encourage well-written WordPress.

Home Page: https://stevegrunwell.com/blog/wp-enforcer/

License: MIT License

Shell 100.00%
wordpress coding-standards git-hooks php-codesniffer composer-packages

wp-enforcer's Introduction

WP Enforcer

GitHub release GitHub license

When writing for a platform as big as WordPress – especially in a team environment – a consistent set of coding standards is vital. Having multiple developers committing code using a mixture of spaces and tabs, inconsistent formatting, or otherwise varying coding styles can lead to headache-inducing merge conflicts.

There are a number of great tools available to check against established coding standards (namely PHP_CodeSniffer), but setting them up on a project can be a pain. WP Enforcer is designed to make this setup a breeze.

How does it work?

WP Enforcer uses Git Hooks to automatically run PHP_CodeSniffer every time a developer tries to make a commit; if the code isn't up-to-snuff, the commit will be rejected and the developer will get a message explaining what needs to be fixed.

Note: If your project already has messy standards you may not want to make WP Enforcer mandatory until after you've cleaned up the codebase or suddenly commits will be quite painful! See Adding to an Existing Project for an easy work-around.

Installation

The easiest way to install WP Enforcer is via Composer:

$ composer require --dev stevegrunwell/wp-enforcer

This will add WP Enforcer to your composer.json file and install the WP Enforcer package.

Next, you'll need to run the WP Enforcer installation script to copy the Git hooks into your local repository:

$ ./vendor/bin/wp-enforcer

If you'd like to require WP Enforcer for all developers on your project, you can add the following scripts to your composer.json file to have WP Enforcer automatically set up Git hooks on composer install and composer update (for more information, please see Composer Scripts):

{
	"scripts": {
		"post-install-cmd": [
			"wp-enforcer"
		],
		"post-update-cmd": [
			"wp-enforcer"
		]
	}
}

Writing for WordPress.com VIP

WP Enforcer includes built-in support for the WordPress.com VIP coding standards, simply add --vip when you run the wp-enforcer command (including in the post-install and post-update commands).

If you need to switch an existing project to the WordPress.com VIP coding standards, you can add the following to your project's phpcs.xml file (replacing "WordPress-Extra", if it's set):

<rule ref="WordPress-VIP" />

Using a custom ruleset

If you want to use a custom XML configuration file with PHP_CodeSniffer, add --ruleset=[FILE] when you run the wp-enforcer command.

Configuration

PHP_CodeSniffer allows you to define a custom, default ruleset by creating a phpcs.xml file in your project root. WP Enforcer will automatically create a phpcs.xml file in your project if one doesn't already exist, but you can edit this to exclude certain files/directories, ignore specific rules, or load additional rulesets.

For a complete list of configuration options, see the PHP_CodeSniffer ruleset.xml standard.

Adding to an Existing Project

Adding coding standards to an existing project can be a painful process. WP Enforcer will only run the Git hook against changed files, but if the current codebase is a mess, making even standards cleanup commits becomes a giant hassle.

If you're adding WP Enforcer to a current project, it's recommended to follow the installation instructions above in a new clean-up branch, then remove the pre-commit hook:

$ rm .git/hooks/pre-commit

Reminder: Use the rm command with care; double-check everything you're passing to it before you execute, as we'd hate to see you lose valuable repository information (or worse: your operating system).

This leaves you with a copy of PHP_CodeSniffer, a phpcs.xml file, and the WordPress Coding Standards but removes the "don't commit improperly formatted code" restrictions. While still in your clean-up branch, work through the codebase and clean up coding standards, running PHP_CodeSniffer manually as needed:

$ ./vendor/bin/phpcs

You may also run PHP Code Beautifier and Fixer, which can fix many common indentation and formatting issues automatically, using the PHP Code Beautifier and Fixer (phpcbf) command:

$ ./vendor/bin/phpcbf

Once CodeSniffer comes back clean, re-run the WP Enforcer installation script to restore the Git hook, forcing all future commits to be clean:

$ ./vendor/bin/wp-enforcer

Finally, merge your clean-up branch into master, kicking your project's coding standards compliance into high gear.

Bypassing WP Enforcer

If it's necessary to bypass WP Enforcer's sniffs for a particular commit (for instance, you're committing small chunks of a file that has older, non-compliant code) you may do so by using git commit --no-verify.

It's recommended that WP Enforcer is only bypassed when making changes in legacy files that simply haven't been cleaned up yet. For more on integrating WP Enforcer with legacy projects, please see Adding to an Existing Project.

wp-enforcer's People

Contributors

bhubbard avatar bradp avatar bswatson avatar leewillis77 avatar stevegrunwell 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

wp-enforcer's Issues

[docs] Install for --dev

Install docs say to run:

composer require stevegrunwell/wp-enforcer

But wouldn't it be more appropriate to composer require stevegrunwell/wp-enforcer --dev?

How about a github bot?

I would love to see a cloud function that can do the following:

  • Verify the repo is using latest version of WP Enforcer, maybe auto update it.
  • Submit a automatic pull request of phpcbf changes. I would see it working similar to how imgbot works.

Script overwrites installed coding standards

Problem

In version 0.5.0, when I run wp-enforcer it removes the standards already set for a project.

Description

I have multiple coding standards installed in my project (WP Coding Standards and PHPCompatibility) and a script to set them for use with the project's PHPCS. When I run wp-enforcer, it only checks for wp-coding-standards/wpcs and removes the other standard on every run, which causes checks to fail because PHPCS can't find the expected sniffs.

Possible Resolution

It would be nice for WP Enforcer to honor the existing config and not remove the standards set, either by not forcing the standards inside the script or by appending wpcs to the installed_paths only if it's not present already.

[docs] Update installation instructions Re: Composer Commands

If you'd like to require WP Enforcer for all developers on your project, you can add the following commands to your composer.json file to have WP Enforcer automatically set up Git hooks on composer install and composer update:

"post-install-cmd": [
	"wp-enforcer"
],
"post-update-cmd": [
	"wp-enforcer"
]

Please state the outcomes provided in performing this action, otherwise you may be proliferating the disease known as copypasta.

Add option to use WordPress.com VIP coding standards

I mentioned it in a tweet to @tarendai this evening, so I'd might as well add it here:

Considering PHP_CodeSniffer supports the VIP Coding Standards, it wouldn't take much effort to create a branch that uses those standards. Alternately, a --vip option during installation could make WP Enforcer helpful for VIP projects.

Release 1.0.0

http://semver.org/#how-do-i-know-when-to-release-100

If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.

Sample phpcs.xml / hooks should include explicit paths

Context:

  • I have phpcs installed already (/usr/local/bin/phpcs).
  • I have (an older version) of the WP coding standards installed (Which happens not to have WordPress-Docs)

When setting up with an out-of-the-box install, everything fails since the pre-commit hook is using my "normal" phpcs which knows nothing about the standards under vendor, specifically it knows nothing about WordPress-Docs

I can see the arguments both ways about global installs vs locals, but I think if we're taking the time to download the dependencies then we should set things up to use them. This would allow different projects to use different versions if required.

This would mean prefixing phpcs in the pre-commit hook with an explicit patch, and I think also providing a path for the standards to use, not just the name.

Process only PHP files / add additional excludes

Out-of-the-box the plugin won't let me commit the changes to add it to my repo. It looks like it's running PHPCS over everything, not just PHP files. I'm not sure whether the desired solution here would just be to add those files (Which should be common to anyone using this?) to the same phpcs.xml as excluded paths? or to make PHPCS only called on PHP files? Or is there something funky going on for me?

$ git commit -m 'Trialling WP enforcer for pre-commit awesomeness'
Running PHP CodeSniffer...

FILE: ...t/.gitignore
--------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------
 1 | WARNING | No PHP code was found in this file and short open tags are not
   |         | allowed by this install of PHP. This file may be using short
   |         | open tags but PHP does not allow them.
--------------------------------------------------------------------------------

FILE: ...t/composer.json
--------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------
 1 | WARNING | No PHP code was found in this file and short open tags are not
   |         | allowed by this install of PHP. This file may be using short
   |         | open tags but PHP does not allow them.
--------------------------------------------------------------------------------

FILE: ...t/composer.lock
--------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------
 1 | WARNING | No PHP code was found in this file and short open tags are not
   |         | allowed by this install of PHP. This file may be using short
   |         | open tags but PHP does not allow them.
--------------------------------------------------------------------------------

FILE: ...t/phpcs.xml
--------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------
 1 | WARNING | No PHP code was found in this file and short open tags are not
   |         | allowed by this install of PHP. This file may be using short
   |         | open tags but PHP does not allow them.
--------------------------------------------------------------------------------

Time: 17ms; Memory: 4Mb

Setup fails when hooks directory does not exist

cp ~/test/.git/hooks/pre-commit: No such file or directory
chmod ~/test/.git/hooks/pre-commit: No such file or directory
Copying pre-commit hook

WP Enforcer installed successfully!

Installation of hooks failed to install, but still reported a success.

Restrict hook to the changed files

Good recommendation from @bswatson:

When the Git hook runs it should only be run on files that have changed. This will reduce friction when implementing into an existing codebase as a file won't get checked (or at least not flagged) until something inside it has changed. Developers can still run ./vendor/bin/phpcs to scan the entire codebase, but this fix will make WP Enforcer much less of a "get everything in order before this is useful" tool.

Not working!

I added a few problems in my code to see if it works, but I am commiting code and nothing happens.
I am using Mac, testing a plugin and I have installed it just the way you wrote and added extra config in the composer.json.

What could be wrong?

Automated testing

As the project is picking up traction I'm regretting not learning how to unit test Bash scripts earlier. While the scripts may be more integration tests than pure unit tests, I'd love to get some coverage on our various options and features.

Checker prevents adding image files to repo

Possibly related to #12, when I have WP Enforcer enabled, I'm prevented from adding images to my Git repo. PHPCS reports multiple errors, most noticeably "mixed line endings" and "ASP style opening tags" in the files. I have theInternal.NoCodeFound rule added but unless I also add an exclude-pattern for those files it still tries to check them, which is super frustrating.

This happens in my theme with screenshot.png and some JPEG images bundled with it.

Require latest version of WPCS always

Currently composer.json has a hard dependency on WPCS, and is using 0.10.0. Because they're not at a 1.0 and the current rule is ^ this change will not be automatic for minor updates (only one a major lands will that be true). As a result, we need to update to the new standard manually.

Add a "Help" option

As we start introducing options (see #6, #7) a "help" option (-h and --help) need to be implemented so people can see the available options from the CLI before executing the installation script.

No output from wp-enforcer setup script

When running the install script on OS X 10.11.1 I get no output from the shell script. It also replaces the pre-commit hook file entirely without asking.

screenshot 2015-12-16 23 40 01

Here is a debug output of the script:

+ readonly VERSION=0.4.0
+ VERSION=0.4.0
+++ dirname ./vendor/bin/wp-enforcer
++ cd ./vendor/bin
++ pwd -P
+ readonly BIN=/Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/vendor/bin
+ BIN=/Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/vendor/bin
+++ readlink ./vendor/bin/wp-enforcer
++ dirname ../stevegrunwell/wp-enforcer/bin/wp-enforcer
+ readonly DIR=/Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/vendor/bin/../stevegrunwell/wp-enforcer/bin
+ DIR=/Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/vendor/bin/../stevegrunwell/wp-enforcer/bin
++ pwd
+ readonly PROJECT=/Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data
+ PROJECT=/Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data
+ vip=false
++ getopt -o h,v -l help,version,vip --
+ options=' -- h,v -l help,version,vip --'
+ '[' 0 -gt 0 ']'
+ echo 'Installing WP Enforcer...'
Installing WP Enforcer...
+ [[ ! -d /Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/.git ]]
+ [[ ! -d /Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/.git/hooks ]]
+ [[ -f /Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/.git/hooks/pre-commit ]]
+ cmp /Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/vendor/bin/../stevegrunwell/wp-enforcer/bin/hooks/pre-commit /Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/.git/hooks/pre-commit
+ echo 'pre-commit hook already exists, skipping'
pre-commit hook already exists, skipping
+ [[ ! -f /Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/phpcs.xml ]]
++ /Users/brandoncamenisch/Sites/Vagrants/vvv/www/ten/wordpress/wp-content/plugins/vehicle-data/vendor/bin/phpcs --config-show
++ grep installed_paths:
+ installed_paths='installed_paths: ../../wp-coding-standards/wpcs'
+ [[ installed_paths: ../../wp-coding-standards/wpcs != \i\n\s\t\a\l\l\e\d\_\p\a\t\h\s\:\ \.\.\/\.\.\/\w\p\-\c\o\d\i\n\g\-\s\t\a\n\d\a\r\d\s\/\w\p\c\s ]]
+ echo

+ echo 'WP Enforcer installed successfully!'
WP Enforcer installed successfully!
+ exit 0

Support custom PHPCS rulesets

In addition to --vip, wp-enforcer should support custom rulesets.

One of my clients has a project.ruleset.xml file in the root directory that our team should be using on their project.

Maybe a --customruleset='...' option?

Rewrite for the PHP CLI?

I'm considering rewriting the main wp-enforcer Bash script in PHP. It would still be run the same way, but rather than dealing with a very procedural scripting language we could a) leverage the PHP CLI Tools package maintained by the WP-CLI team, b) make testing the code for this [very WordPress-specific tool] way easier (see #16), and generally make the script more portable (considering the package is installed via Composer and thus only available in environments that can already execute code via the PHP CLI).

Anyone want to either talk me off that ledge or convince me to take the plunge before we hit a 1.0.0?

Cc: @bswatson @daveross @ericmann

Update README to signify where to add custom scripts

The README currently suggests adding the following "commands" to the package manifest, but it doesn't indicate where they should land:

"post-install-cmd": [
	"wp-enforcer"
],
"post-update-cmd": [
	"wp-enforcer"
]

As a total n00b using composer I thought these were some kind of built-in. It wasn't until later I realized I should've added them to the scripts section and not in the root of the manifest. I imagine a quick GH search for apps using this package will turn up the same.

Restrict hook to the changed code

Would it be possible to modify the hook to check only the code that was added in a given commit (instead of checking changed files as in #1)? I think this would reduce friction even more when using wp-enforcer into an existing codebase.

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.