Showing posts with label composer. Show all posts
Showing posts with label composer. Show all posts

Is Your Drupal Code Adheres to Coding Standards and Best Practices?

Following a coding standards and adhere to a best practices in any development environment is crucial to keep the code base consistent and every developer is aware of the default ways of development. For Drupal, there is this Coder module which helps to make sure your Drupal codes following the standard conventions and practices.

Before we install the Coder module, let's obtain some Drupal codes to be validated. We'll be using sample codes from Drupal Examples.
$ git clone git://git.drupal.org/project/examples.git drupal-examples

Install the Coder module using Composer. Note that I've tried installing using Drush but I still prefer the former way which is more suitable for general PHP library management.
$ composer global require drupal/coder

Changed current directory to /home/ang/.composer
Using version ^8.2 for drupal/coder
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing squizlabs/php_codesniffer (2.3.2)
    Downloading: 100%         

  - Installing drupal/coder (8.2.2)
    Cloning c08506a332235d5485c79231639e9577b8c4d332

Writing lock file
Generating autoload files

Checking the installed PHP CodeSniffer (phpcs) and PHP Code Beautifier and Fixer (phpcbf) program.
$ ll ~/.composer/vendor/bin/
total 0
lrwxrwxrwx. 1 ang ang 43 May 27 18:13 phpcbf -> ../squizlabs/php_codesniffer/scripts/phpcbf*
lrwxrwxrwx. 1 ang ang 42 May 27 18:13 phpcs -> ../squizlabs/php_codesniffer/scripts/phpcs*

However, both programs are not within Bash's program search path. Hence, we have to add it manually.
$ export PATH=$PATH:$HOME/.composer/vendor/bin

To make it permanent, just put the above line into ~/.bashrc or ~/.profile file.
$ echo "export PATH=$PATH:$HOME/.composer/vendor/bin" > ~/.bashrc

To verify our path has been set up correctly.
$ which phpcs
~/.composer/vendor/bin/phpcs

To test whether a sample code is adhere to Drupal coding standard. As shown below, the phpcs program can't find the Drupal coding standard rules file.
$ phpcs --standard=Drupal drupal-examples/action_example/action_example.module
ERROR: the "Drupal" coding standard is not installed. The installed coding standards are PSR1, PHPCS, Squiz, PSR2, Zend, PEAR and MySource

Set the path to the Drupal coding standard rules path and verify again.
$ phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
Config value "installed_paths" added successfully

$ phpcs -i
The installed coding standards are PSR1, PHPCS, Squiz, PSR2, Zend, PEAR, MySource, Drupal and DrupalPractice

Let's try again. Surprisingly the official Drupal examples code contains coding standards violation!
$ phpcs --standard=Drupal action_example/action_example.module 

FILE: ...ng/drupal-examples/action_example/action_example.module
----------------------------------------------------------------------
FOUND 13 ERRORS AND 2 WARNINGS AFFECTING 15 LINES
----------------------------------------------------------------------
 228 | ERROR   | [ ] Parameter comment must end with a full stop
 276 | ERROR   | [ ] Parameter comment must end with a full stop
 283 | ERROR   | [ ] Type hint "array" missing for $context
 285 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 286 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 287 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 288 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 289 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 290 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 291 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 292 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 293 | ERROR   | [x] Line indented incorrectly; expected 3 spaces, found 2
 343 | ERROR   | [ ] Type hint "array" missing for $context
 345 | WARNING | [ ] The use of function dsm() is discouraged
 346 | WARNING | [ ] The use of function dsm() is discouraged
----------------------------------------------------------------------
PHPCBF CAN FIX THE 9 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 429ms; Memory: 7.5Mb

Using Multiple Drush Version For All Users

Drush, is the wonderful command line utility to manage Drupal site. However, on some occasion, you will need to install different Drush version in your local development machine to test out different features.

Following the official installation guide, we're going to setup global accessible Drush installation. The benefit of using such method is that any user can run the Drush command. Note that I'm currently using Fedora Rawhide (F22) but this should be applicable to all GNU/Linux distros.

First, make sure you've already install the Git version control tool. If not, just install it.
$ sudo yum install git php php-pear
$ sudo pear install Console_Table

Next, we'll clone the Drush from the Github repository. Rather than following the convention of installing it in /usr/local directory, I opt for /opt directory to differentiate packages or software installed by distribution and myself. Although the Drush command still depends on PHP binary from the default distribution.
$ sudo git clone https://github.com/drush-ops/drush.git /opt/drush
Cloning into '/opt/drush'...
remote: Counting objects: 29817, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 29817 (delta 6), reused 0 (delta 0), pack-reused 29798
Receiving objects: 100% (29817/29817), 11.04 MiB | 1.75 MiB/s, done.
Resolving deltas: 100% (16973/16973), done.
Checking connectivity... done.

Following that, we'll need to find the available Drush versions. This is through git tag command.
$ cd /opt/drush
$ git tag
1.0.0+drupal5
1.0.0+drupal6
1.0.0-beta1+drupal5
1.0.0-beta2+drupal5
1.0.0-beta3+drupal5
1.0.0-beta4+drupal5
1.0.0-rc1+drupal5
1.0.0-rc1+drupal6
1.0.0-rc11+drupal6
1.0.0-rc2+drupal5
1.0.0-rc2+drupal6
1.0.0-rc2+drupal7
1.0.0-rc3+drupal5
1.1.0+drupal5
1.1.0+drupal6
1.2.0+drupal5
1.2.0+drupal6
1.3.0+drupal5
1.4.0+drupal5
2.0.0
2.0.0-alpha1+drupal5
2.0.0-alpha1+drupal6
2.0.0-alpha1+drupal7
2.0.0-alpha2+drupal5
2.0.0-alpha2+drupal6
2.0.0-alpha2+drupal7
2.0.0-rc1
2.1.0
3.0.0
3.0.0-alpha1
3.0.0-beta1
3.0.0-rc1
3.0.0-rc2
3.0.0-rc3
3.0.0-rc4
3.1.0
3.2.0
3.3.0
4.0.0
4.0.0-rc1
4.0.0-rc10
4.0.0-rc3
4.0.0-rc4
4.0.0-rc5
4.0.0-rc6
4.0.0-rc7
4.0.0-rc8
4.0.0-rc9
4.1.0
4.2.0
4.3.0
4.4.0
4.5.0
4.5.0-rc1
4.6.0
5.0.0
5.0.0-rc1
5.0.0-rc2
5.0.0-rc3
5.0.0-rc4
5.0.0-rc5
5.1.0
5.10.0
5.11.0
5.2.0
5.3.0
5.4.0
5.5.0
5.6.0
5.7.0
5.8.0
5.9.0
6.0.0
6.0.0-beta1
6.0.0-rc1
6.0.0-rc2
6.0.0-rc3
6.0.0-rc4
6.1.0
6.1.0-rc1
6.2.0
6.3.0
6.4.0
6.5.0
7.0.0-alpha1
7.0.0-alpha2
7.0.0-alpha3
7.0.0-alpha4
7.0.0-alpha5
7.0.0-alpha6
7.0.0-alpha7
7.0.0-alpha8
7.0.0-alpha9
debian/4.3-1
debian/4.4-1
debian/4.4-2
debian/4.5-2
debian/5.0dev-1

Let's try with version 6.5 first using the git checkout command.
$ sudo git checkout 6.5.0                                                                                                  
Note: checking out '6.5.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 0a7918a... Prep for 6.5.0

Later, we need to create the soft link to the drush command and verify that our link works. Instead of using /usr/local/bin, we'll put the symbolic link to /usr/bin directory. More on this later.
$ sudo ln -s /opt/drush/drush /usr/bin/drush
$ which drush
/usr/local/bin/drush

Checking our Drush version.
$ drush --version
 Drush Version   :  6.5.0 

To switch another Drush version, for example version 5.1, we'll using the git checkout command again.
$ sudo git checkout 5.1.0
Previous HEAD position was 0a7918a... Prep for 6.5.0
HEAD is now at 1583e0d... Prep for 5.1

$ drush --version
drush version 5.1

How about the latest version of 7. As shown below, version 7 uses Composer to install all its dependencies.
$ sudo git checkout 7.0.0-alpha9
Previous HEAD position was 1583e0d... Prep for 5.1
HEAD is now at f10919a... Prep for 7.0.0-alpha9.

$ drush --version
Unable to load autoload.php. Drush now requires Composer in order to install its dependencies and autoload classes. Please see README.md

Installation of Composer, dependency management for PHP is straight forward although executing downloaded Bash script directly is not recommended.
$ sudo mkdir /opt/composer
$ cd /opt/composer/

$ sudo sh -c 'curl -sS https://getcomposer.org/installer | php'
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /opt/composer/composer.phar
Use it: php composer.phar

Now similarly, we'll create the symbolic link for composer.phar in /usr/bin directory.
$ sudo ln -s /opt/composer/composer.phar /usr/bin/composer

$ composer --version
Composer version 1.0-dev (eadc167b121359986f542cc9cf976ecee3fcbf69) 2015-03-02 18:20:22

Why we need to put in /usr/bin directory instead of /usr/local/bin directory? The reason is the default $PATH for sudo command, as shown below, does not includes the /usr/local/bin directory.
$ sudo bash
# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin

Since we've installed Composer and satisfied the Drush version 7 dependency. We'll check our installation again.
$ cd /opt/drush
$ sudo composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
......

Lastly, check our Drush version again.
$ drush --version                                                                                                          
 Drush Version   :  7.0.0-alpha9 

Hence, by using Git, we can easily install and switch to different Drush version within one installation.