How to Apply Coding Standard on PHP Snippets in Markdown Files?

Written by Tomáš Votruba / Original link on Sep. 14, 2020

This feature was contributed to ECS 8.3 by samsonasik. Thank you!

We try to make new features as intuitive to use as possible, so you have to learn as less a possible to use them. Have more fun.

You already know ECS command check:

vendor/bin/ecs check src

# to change the code
vendor/bin/ecs check src --fix

check but for Markdown Files

How to apply the same coding standard to markdown files?

Just use check-markdown command instead of check:

vendor/bin/ecs check-markdown

You can use multiple files or directories:

# do you have multiple files?
vendor/bin/ecs check-markdown packages

# or target names only?
vendor/bin/ecs check-markdown packages/**/

How to fix the content? Just add --fix:

vendor/bin/ecs check-markdown --fix

All the rules that you defined in ecs.php will be applied the same way they're applied to PHP code.

How does it Look in Practise?


Composer Scripts Tip

How to add all this to your workflow? Another command to run every time? Nah, that's too memory locking and tedious.

Instead, we can extend composer scripts in composer.json:

     "scripts": {
-        "check-cs": "vendor/bin/ecs check",
+        "check-cs": [
+           "vendor/bin/ecs check",
+           "vendor/bin/ecs check-markdown",
+        ],
-        "fix-cs": "vendor/bin/ecs check --fix"
+        "fix-cs": [
+            "vendor/bin/ecs check --fix",
+            "vendor/bin/ecs check-markdown --fix",
+        ]

How does it change your workflow? Not at all! You can still use the same commands:

composer checks-cs
composer fix-cs

Just now they're much smarter ;)

Happy coding!


« Not sure what to read? Buy what people you follow recommend - Moment.js, thank you for your service »