Coding standards and Sage

sage9

#1

Hi All,

I’m struggling to get my head around how practically coding standards work in the context of Sage and my own development environment.

I’ve now built quite a few websites with Sage9, I use PHPStorm as my IDE. I’m aware of various coding standards; phpcs, psr-0, psr-2 and psr-4. Plus there is terminology like psr-4 autoloading, code_sniffing and so on.

Am I right in thinking that Sage uses psr-2 for code and psr-4 autoloading? Can anyone explain the difference - are these two different things?

What is psr-2 exactly, is it just about the number of spaces for indents, end of line and trailing whitespace as defined in .editorconfig or is it more than that?

Sometimes in my IDE when editing .php or .js files I get warning messages about the number of spaces used for indents, I usually just click OK to accept it’s recommendation but would this be coming from PHPStorm inperpreting the .editorconfig file?

Finally, what should I be doing about testing? I have noticed that in composer.json, I have the following:

"require-dev": {
    "squizlabs/php_codesniffer": "^2.8.0",
    "roots/sage-installer": "~1.3"
  },
  "scripts": {
    "test": ["phpcs"],
    "post-create-project-cmd": [
      "Roots\\Sage\\Installer\\ComposerScript::postCreateProject"
    ]
  }

What relation does this have if any to coding standards and testing and how should I be using something like php_codesniffer?

Thanks in advance for any help with this.
Kevin


#2

This is correct

This isn’t really the appropriate place for general PHP questions. Not trying to be rude, we can’t be the source for all of the information for all the pieces that make up our projects :smiley:

Probably.

This is in place primarily for contributing to the open-source development of Sage. You can run composer test to run PHPCS to see if the coding standards pass in your PHP files.

There’s also stylelint and ESLint that are included for enforcing standards in your styles and JS - the configuration for those can be found here and here.


#3

The first result of a Google search for “psr-2” is the official documentation: https://www.php-fig.org/psr/psr-2/ Documentation for all PSRs can be found on that site which explains them pretty well.


#4

Yeah OK I think I get it! From that link @benword it seems as though psr-4 is only to do with autoloading. I was under the impression that psr-2 and psr-4 were just 2 alternatives for the same thing.

Thanks for your help! Just one more question, when I run composer test I get a bunch of errors which is good, but also a message that I can fix them automatically using phpcbf - is it possible to send additional arguments to composer test to get it to use phpcbf and fix the errors automatically?

Kevin


#5

OK, I figured it out… I just added "fix": ["phpcbf"], to the scripts part of composer and ran composer fix - worked like a charm… small victories! :slightly_smiling_face: