rtCamp notes, day 5/undefined

PHPCS

PHPCS is a tool that helps detect violations of pre-defined coding standards. It also includes an additional tool that can automatically correct those violations.

What are coding standards?

Coding Standards refers to a set of rules or guidelines consisting of coding styles, Naming conventions and techniques created to promote uniformity and readability of the code and make it easier to collaborate as well.

The official published coding standards for PHP is PHP Standard Recommendation (PSR),

I’ll be referring a list of PSR with their usage here

We tend to follow these coding standards not only for the collaboration part, but they also help improving our coding standards in our brains and making us more efficient.

Installing PHPCS

composer global require "squizlabs/php_codesniffer=*"

Usage

Using the below commands we can use the functionality of PHPCS

phpcs <filename>

This would scan the file you specified in filename and provide you the results for the same.

If you want all the files in a folder to be scanned you can pass . as your file name,

for e.g.

phpcs .

This would return you an output for all the files under your current working directory

PHPCBF

phpcbf is script to automatically fix as many sniff violations as possible.

PHPCBF stands for PHP Code Beautifier and Fixer

Usage

Using the below commands we can use the functionality of PHPCBF

phpcbf <filename>

This would fix all the sniff violations possible and return you with the information.

If you want all the files in a folder to be fixed you can pass . as your file name,

for e.g.

phpcbf .

This would scan all the PHP files inside that folder and try to fix them all.

GIT

I learned about some more git concepts today like merging and merge conflicts

A merge conflict usually happens when you are collaborating on a project and two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other.

Git cannot come to a decision to which change to keep, therefore it needs human attention and puts it to you.

Some commands to look out on a merge conflict:

git log --merge -> produce a log with conflict messages in it

Read about Github actions and .yml/.yaml for workflow actions.

These are event driven and can be defined to be triggered during different events occured.

name: GitHub Actions Demo
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v3
      - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
      - run: echo "🍏 This job's status is ${{ job.status }}."

on -> event on which the actions must be triggered

name -> name of the workflow

jobs -> jobs to process

runs-on -> specify on which platform the jobs should be processed

steps -> steps which should be processed one by one

Some important learning aspects from today’s day:

  • Got to know about Code Standards and why they are important
  • Using PHPCS
  • Using PHPCBF
  • Git merge conflicts
  • Git Actions
  • Git Actions Workflow

Need to work more on:

  • PHPCS with WordPress

That’s pretty much how my day today went at rtCamp, looking towards more wonderful days.

Regards

Aryan Jasala

WordPress Engineer Trainee

Leave a Reply

Your email address will not be published. Required fields are marked *