Free entangled tree roots image

PHPCS Anywhere!

Something that I do often is run PHPCS on code I’m working on, almost always inside a git repository. Even more likely is that PHPCS was installed via composer, which means it will live in $GIT_ROOT/vendor/bin. So I always end up doing something like ../../../vendor/bin/phpcs file.php which is hugely annoying.

Which is why I made this monstrosity:

# PHPCS
function phpcs() {
        if git rev-parse --git-dir > /dev/null 2>&1; then
                git_root=$(git rev-parse --show-toplevel 2>/dev/null)

                # Check if vendor/bin/phpcs exists
                if [[ -x "$git_root/vendor/bin/phpcs" ]]; then
                        # Call the local vendor/bin/phpcs
                        "$git_root/vendor/bin/phpcs" "$@"
                fi
        else
                # Fall back to the system's default phpcs
                command phpcs "$@"
        fi
}

# PHPCBF
function phpcbf() {
        if git rev-parse --git-dir > /dev/null 2>&1; then
                git_root=$(git rev-parse --show-toplevel 2>/dev/null)

                # Check if vendor/bin/phpcbf exists
                if [[ -x "$git_root/vendor/bin/phpcbf" ]]; then
                        # Call the local vendor/bin/phpcbf
                        "$git_root/vendor/bin/phpcbf" "$@"
                fi
        else
                # Fall back to the system's default phpcbf
                command phpcbf "$@"
        fi
}Code language: Bash (bash)

Basically, what this is doing is any time I run phpcs or phpcbf it will first check if I am inside a git repository. If I am, and $GIT_ROOT/vendor/bin/phpcs exists, it will automatically find it and use it.

Saves me seconds a day. SECONDS! Maybe you can find it useful as well. Or not. Who knows.

Other Posts Not Worth Reading

Hey, You!

Like this kind of garbage? Subscribe for more! I post like once a month or so, unless I found something interesting to write about.


Comments

Leave a Reply