Giter Site home page Giter Site logo

joomla / cms-coding-standards Goto Github PK

View Code? Open in Web Editor NEW
9.0 8.0 9.0 55 KB

Extended Joomla Coding Standards definition for use in the Joomla CMS environment

License: GNU General Public License v2.0

joomla php-codesniffer phpcs ruleset joomla-cms-environment

cms-coding-standards's Introduction

The Joomla project has migrated all its code to use PSR-12 codestyle instead of a custom version. If you are using this codestyle, you should also migrate to PSR-12. This codestyle will not get any further maintenance and is therefore archived since March 2024.

cms-coding-standards

Extended Joomla Coding Standards definition for use in the Joomla CMS environment

This repository includes the Joomla CMS coding standard definition for PHP Codesniffer. The PHP_CodeSniffer standard will never be 100% accurate, but should be viewed as a strong set of guidelines while writing software for Joomla.

See Joomla coding standards documentation at https://developer.joomla.org/coding-standards.html

If you want to contribute and improve this documentation, you can find the source files in the manual section of the master branch https://github.com/joomla/coding-standards/tree/master/manual

Requirements

Installation via Composer

Add "joomla/cms-coding-standards": "~1.0" to the require-dev block in your composer.json and then run composer install.

{
    "require-dev": {
		"joomla/cms-coding-standards": "~1.0"
	}
}

Alternatively, you can simply run the following from the command line:

composer global require squizlabs/php_codesniffer "~2.8"
composer require joomla/cms-coding-standards "~1.0"

Running

You can use the installed Joomla standard like:

phpcs --standard=Joomla-CMS path/to/code

Alternatively if it isn't installed you can still reference it by path like:

phpcs --standard=path/to/joomla/cms-coding-standards path/to/code

Selectively Applying Rules

NOTE: this should be an exact copy of the "Selectively Applying Rules" section of the main Joomla Coding Standards README and is here only for reference

For consuming packages there are some items that will typically result in creating their own project ruleset.xml, rather than just directly using the Joomla ruleset.xml. A project ruleset.xml allows the coding standard to be selectivly applied for excluding 3rd party libraries, for consideration of backwards compatability in existing projects, or for adjustments necessary for projects that do not need php 5.3 compatability (which will be removed in a future version of the Joomla Coding Standard in connection of the end of php 5.3 support in all active Joomla Projects).

For information on selectivly applying rules read details in the PHP CodeSniffer wiki

Common Rule Set Adjustments

The most common adjustment is to exclude folders with 3rd party libraries, or where the code has yet to have coding standards applied.

<!-- Exclude folders not containing production code -->
	<exclude-pattern type="relative">build/*</exclude-pattern>
	<exclude-pattern type="relative">tests/*</exclude-pattern>

	<!-- Exclude 3rd party libraries. -->
	<exclude-pattern type="relative">libraries/*</exclude-pattern>
	<exclude-pattern type="relative">vendor/*</exclude-pattern>

Another common adjustment is to exclude the camelCase format requirement for "Classes, Functions, Methods, Regular Variables and Class Properties" the essentially allows for B/C with snake_case variables which were only allowed in the context of interacting with the database.

 <rule ref="Joomla">
  <exclude name="Joomla.NamingConventions.ValidFunctionName.ScopeNotCamelCaps"/>
  <exclude name="Joomla.NamingConventions.ValidVariableName.NotCamelCaps"/>
  <exclude name="Joomla.NamingConventions.ValidVariableName.MemberNotCamelCaps"/>
  <exclude name="Joomla.NamingConventions.ValidFunctionName.FunctionNoCapital"/>
 </rule>

Old Protected method names were at one time prefixed with an underscore. These Protected method names with underscores are depreceated in Joomla projects but for B\C reasons are still in the projects. As such excluding the MethodUnderscore sniff is a common ruleset adjustment

 <rule ref="Joomla">
  <exclude name="Joomla.NamingConventions.ValidFunctionName.MethodUnderscore"/>
  <exclude name="Joomla.NamingConventions.ValidVariableName.ClassVarHasUnderscore"/>
 </rule>

Using example rulesets that Selectively Applying Rule

You have to tell you can tell PHPCS where the example ruleset folder is (i.e. install them in PHPCS)

phpcs --config-set installed_paths /path/to/joomla/coding-standards/Example-Rulesets

Note: the composer scripts will run when the standard is installed globally, but not when it's a dependancy. As such, you may want to run PHPCS config-set. When you run PHPCS config-set it will always overwrite the previous values. Use --config-show to check previous values before using --config-set So instead of overwriting the existing paths you should copy the existing paths revealed with --config-show and add each one seperated by a comma: phpcs --config-set installed_paths [path_1],[path_2],[/path/to/joomla-coding-standards],[/path/to/joomla/coding-standards/Example-Rulesets]

cms-coding-standards's People

Contributors

hackwar avatar mbabker avatar photodude avatar wilsonge avatar zero-24 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cms-coding-standards's Issues

Fix Branching

The branching of this repository should be fixed.

  • Create a 1.x branch from what is presently master

With that done, the following diff should be applied to the composer.json file:

diff --git a/composer.json b/composer.json
index eda994c..095cf8a 100644
--- a/composer.json
+++ b/composer.json
@@ -17,14 +17,14 @@
        }
     },
     "require": {
-        "php": ">=5.3.10",
-        "joomla/coding-standards": "~2.0 || ~3.0"
+        "php": "^5.3.10|^7.0",
+        "joomla/coding-standards": "^2.0|^3.0"
     },
     "require-dev": {
         "phpunit/phpunit": "^4.8.7"
     },
     "suggest" : {
-        "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically."
+        "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 This Composer plugin will sort out the PHPCS 'installed_paths' automatically."
     },
     "support"    : {
         "issues": "https://github.com/joomla/cms-coding-standards/issues",
@@ -32,7 +32,7 @@
     },
     "extra": {
         "branch-alias": {
-            "dev-master": "1.x-dev"
+            "dev-1.x": "1.x-dev"
         }
     },
     "scripts": {

(Explicitly lists supported PHP branches instead of allowing all versions arbitrarily, changes branch alias for 1.x versions)

  • Tweak the README to explain the branching

This diff will suffice (someone with admin will need to copy the diff from the "edit issue" input because the GitHub parser is breaking at the first set of closing backticks, or just manually apply all changes):

diff --git a/README.md b/README.md
index 5eb1a1f..95a766a 100644
--- a/README.md
+++ b/README.md
@@ -1,17 +1,19 @@
-# cms-coding-standards
+# Joomla! CMS Coding Standards
 Extended Joomla Coding Standards definition for use in the Joomla CMS environment
 
 This repository includes the [Joomla](https://developer.joomla.org) CMS coding standard definition for [PHP Codesniffer](https://github.com/squizlabs/PHP_CodeSniffer).  The PHP_CodeSniffer standard will never be 100% accurate, but should be viewed as a strong set of guidelines while writing software for Joomla.
 
 See Joomla coding standards documentation at [https://developer.joomla.org/coding-standards.html](https://developer.joomla.org/coding-standards.html)
 
-If you want to contribute and improve this documentation, you can find the source files in the manual section of the master branch [https://github.com/joomla/coding-standards/tree/master/manual](https://github.com/joomla/coding-standards/tree/master/manual)
+If you want to contribute and improve this documentation, you can find the source files in the `manual` directory of the `master` branch of the main [Coding Standards](https://github.com/joomla/coding-standards) repository.
 
 ## Requirements
 
 * PHP 5.3+
 * [PHP Codesniffer](https://github.com/squizlabs/PHP_CodeSniffer) 2.9+
+
 or
+
 * PHP 5.6+
 * [PHP Codesniffer](https://github.com/squizlabs/PHP_CodeSniffer) 3.3.0+
 
@@ -22,27 +24,38 @@ Add `"joomla/cms-coding-standards": "~1.0"` to the require-dev block in your com
 ```json
 {
     "require-dev": {
-		"joomla/cms-coding-standards": "~1.0"
-	}
+        "joomla/cms-coding-standards": "~1.0"
+    }
 }

-Alternatively, you can simply run the following from the command line:
+Alternatively, you can run the following from the command line:

composer global require squizlabs/php_codesniffer "~2.8"
composer require joomla/cms-coding-standards "~1.0"

+## Branching Strategy
+
+This repository presently contains two branches:
+
+* 1.x - Compatible with the Joomla! Coding Standards 2.x or 3.x, Joomla! 3.x, and PHP_CodeSniffer 2.x or 3.x
+* master (2.x) - Compatible with the Joomla! Coding Standards 2.x or 3.x, Joomla! 4.x, and PHP_CodeSniffer 2.x or 3.x
+

Running

-You can use the installed Joomla standard like:
+You can use the installed Joomla-CS standard like:

  • phpcs --standard=Joomla-CMS path/to/code
    +sh +phpcs --standard=Joomla-CMS path/to/code +

Alternatively if it isn't installed you can still reference it by path like:

  • phpcs --standard=path/to/joomla/cms-coding-standards path/to/code
    +sh +phpcs --standard=path/to/joomla/cms-coding-standards path/to/code +

Selectively Applying Rules


(Has a couple of minor formatting tweaks, main point is the "Branching Strategy" section added)

- [ ] Merge changes from new `1.x` branch forward to `master` then `4.0-dev`

```sh
git checkout master
git merge 1.x
git checkout 4.0-dev
git merge master

(Self-explanatory I hope on why to do this)

  • Merge the 4.0-dev branch into master and delete the 4.0-dev branch
git checkout master
git merge 4.0-dev
git branch -D 4.0-dev
git push origin :4.0-dev

(This makes this repo's 2.x version the master branch)

  • Fix the branch alias for the (new) master branch to be 2.x
    "extra": {
        "branch-alias": {
            "dev-master": "2.x-dev"
        }
    },

Once complete, this will establish the 1.x branch as the 1.x version of the CMS coding standards (extending the main coding standards repo) as being compatible with the CMS' 3.x releases, and will establish the master branch as the 2.x version of the CMS coding standards (extending the main coding standards repo) as being compatible with the CMS' 4.x releases.

ERROR: Referenced sniff "Joomla.Commenting.FileComment" does not exist

As the title states i get the following error ERROR: Referenced sniff "Joomla.Commenting.FileComment" does not exist when running with this ruleset enabled.

Im using phpcs version 3.4.0 (stable).
And the ruleset provided in the 4.0-dev branch.

I assume I am missing a dependency, but i don't know where to look

License

I know the individual sniffs are following but can you add the license to the entire repo please so that new submissions are done under the following. Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.