{"id":55736,"date":"2022-11-08T18:10:10","date_gmt":"2022-11-08T12:40:10","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55736"},"modified":"2022-11-08T18:10:10","modified_gmt":"2022-11-08T12:40:10","slug":"check-drupal-coding-standard-by-phpcs-in-git-pre-commit-hook","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/check-drupal-coding-standard-by-phpcs-in-git-pre-commit-hook\/","title":{"rendered":"Check Drupal coding standard by phpcs in git pre-commit hook"},"content":{"rendered":"<div><span style=\"color: #24292f; font-family: -apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-55735 alignleft\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/usage-of-phpcs-on-github-via-pull-request-for-drupal-applications-png-788\u00d7443--300x165.png\" alt=\"\" width=\"278\" height=\"153\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/11\/usage-of-phpcs-on-github-via-pull-request-for-drupal-applications-png-788\u00d7443--300x165.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/usage-of-phpcs-on-github-via-pull-request-for-drupal-applications-png-788\u00d7443--1024x564.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/usage-of-phpcs-on-github-via-pull-request-for-drupal-applications-png-788\u00d7443--768x423.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/usage-of-phpcs-on-github-via-pull-request-for-drupal-applications-png-788\u00d7443--624x344.png 624w, \/blog\/wp-ttn-blog\/uploads\/2022\/11\/usage-of-phpcs-on-github-via-pull-request-for-drupal-applications-png-788\u00d7443-.png 1528w\" sizes=\"(max-width: 278px) 100vw, 278px\" \/><\/span><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<div><\/div>\n<p><span style=\"color: #24292f; font-family: -apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;\">In this tutorial, you will learn how to improve the custom code, theme and module, and general code development by using the pre-commit hook on git. Git pre-commit validates Drupal coding standards on files before committing them and returns a message to fix each line of code on the files having errors.<\/span><\/p>\n<div>\n<h2><strong>Follow the next steps to set up the pre-commit git hook with Drupal coding standards:<\/strong><\/h2>\n<ol>\n<li>Create hooks directory on root project.<\/li>\n<li>Create a pre-commit file in the hooks directory.<\/li>\n<li>Add the below pre-commit code to the hooks\/pre-commit file.<\/li>\n<\/ol>\n<blockquote class=\"gmail_quote\">\n<pre>#!\/bin\/bash\r\n\r\nSTANDARD=\"Drupal\"\r\nBIN=\".\/vendor\/bin\"\r\n\r\necho\r\necho \"Drupal Coder pre-commit hook \u2013 commit with the --no-verify option to skip the hook\"\r\necho\r\n\r\n# Check whether PHP_CodeSniffer can be found\r\nif [ ! -f \"$BIN\/phpcs\" ]\r\nthen\r\n  echo \"Drupal Coder not found \u2013 is it installed? 'composer require drupal\/coder'\"\r\n  echo\r\n  exit 1\r\nfi\r\n\r\n# Retrieve staged files\r\nFILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD)\r\n\r\n# Run the sniffer\r\necho \"Running Drupal Coder.\"\r\necho\r\nPHPCS=(\"$BIN\/phpcs\" \"--standard=$STANDARD\" \"--filter=gitstaged\" \"--encoding=utf-8\" \"-p\" \".\")\r\n\"${PHPCS[@]}\"\r\n\r\n# Syntax Error\r\nif [ $? != 0 ]\r\nthen\r\n  echo \"Please fix each violations detected.\"\r\n  echo\r\n  exit 1\r\nfi\r\n\r\n# Syntax OK\r\nif [ $? == 0 ]\r\nthen\r\n  echo \"No violations detected\"\r\n  echo\r\n  exit 0\r\nfi<\/pre>\n<\/blockquote>\n<h2><strong>Add PHP commands on the scripts section of the composer.json file.<\/strong><\/h2>\n<blockquote>\n<pre class=\"gmail_quote\">\"scripts\": {\r\n\u00a0 \u00a0 \u00a0 \"post-install-cmd\": [\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"php -r \\\"copy('hooks\/pre-commit', '.git\/hooks\/pre-commit');\\\"\",\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"php -r \\\"chmod('.git\/hooks\/pre-commit', 0755);\\\"\"\r\n\u00a0 \u00a0 \u00a0 ]\r\n}<\/pre>\n<\/blockquote>\n<div><\/div>\n<div>Now execute <b>Composer install<\/b>\u00a0in your project directory<\/div>\n<\/div>\n<div>Composer install will mv hooks\/pre-commit file to .git\/hooks\/pre-commit.<\/div>\n<div><\/div>\n<div>\n<h2>How to use it<\/h2>\n<\/div>\n<div>\n<pre>git commit -m <span class=\"gmail-pl-s\"><span class=\"gmail-pl-pds\">\"<\/span>The commit message.<span class=\"gmail-pl-pds\">\"<\/span><\/span><\/pre>\n<p>This is going to execute Drupal Coder on each file and returns messages on the terminal in case of finding errors on files.<\/p>\n<div><\/div>\n<\/div>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to improve the custom code, theme and module, and general code development by using the pre-commit hook on git. Git pre-commit validates Drupal coding standards on files before committing them and returns a message to fix each line of code on the files having errors. Follow the next [&hellip;]<\/p>\n","protected":false},"author":1368,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":59},"categories":[3602],"tags":[4862],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55736"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1368"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55736"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55736\/revisions"}],"predecessor-version":[{"id":55756,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55736\/revisions\/55756"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}