<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">
Go to top icon

Code validations using static-review library in Magento

Shubh Sharma Oct 19, 2018

Magento Magento 2.0

Introduction:

Magento2 comes up with some advance tools and plugins, which are really useful for developers to track some common errors during development; I am going to introduce a small tutorial, which helps developers to validate PHP scripts before committing code to Git branch.

Below are some of the validations that can be validated using the described tutorial:

  • LineEndingsReview
  • PhpLeadingLineReview
  • NoCommitTagReview
  • PhpLintReview
  • ComposerLintReview
  • PhpCodeSnifferReview
  • ComposerSecurityReview
Requirements:

  • Git
  • magento2 (2.2.5)
  • sjparkinson/static-review (library)
Installing library sjparkinson/static-review:

To install it in your project, run composer require sjparkinson/static-review in the root of your magento2 installation directory

Command: composer require sjparkinson/static-review

static-review

A Working Example:

  • First, we need to identify the action/observer which requires PHP script validation, So in our example we will validate and run phpunit whenever a developer committed the code in any branch from his local machine
  • Action/Observer can register using Git hooks, If you’ve been using Git for more than a short length of time. you’ll hopefully have heard of Git hooks. There are hooks for pre- and post-commit, pre- and post-update, pre-push, pre-rebase, and so on. Using Static Review library, you can write Git hooks with PHP.
  • To register a hook in Git using Static Review tool follow these steps:
STEP1:

Command: php vendor/bin/static-review.php

php vendor
STEP2:

Command: php vendor/bin/static-review.php help hook:install

4-1
STEP3:


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

5-1
  • Hook has been registered successfully
  • Commit your changes using Git commit command ‘git commit -m “your message” ’. This command automatically scans your changes against validations and displays the result on the screen (refer below screenshot)

6-1
There are many more ways to utilize static review library, we can create our own review rules based on the coding standards, and hopefully this small tutorial will help simplify the life of the developer.

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.