Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Zend Coding Standard 2 #5

Closed
wants to merge 60 commits into from
Closed
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
dbacd12
Import strict ruleset
geerteltink Sep 5, 2018
5863c14
Allow functions inside functions
geerteltink Sep 5, 2018
06a5606
Allow @var for constants
geerteltink Sep 5, 2018
d27454d
Allow @since for noting API additions
geerteltink Sep 5, 2018
646eea1
Allow fancy yoda conditions as a defensive programming approach
geerteltink Sep 5, 2018
fa193f3
Forbid short open tag, but allow short echo tags for use in templates
geerteltink Sep 5, 2018
f1cef3f
Add documentation structure
geerteltink Sep 5, 2018
911065a
Use xmllint to validate against schema
geerteltink Sep 5, 2018
365acdf
Remove copyright in mkdocs config as it's not being used anymore
geerteltink Sep 5, 2018
b3146a6
Remove index.html because it's generated by the theme
geerteltink Sep 5, 2018
5c86655
Disable phpunit tests until there is something to test
geerteltink Sep 5, 2018
ce7ae4a
Use PSR-12 compatible imports
geerteltink Sep 6, 2018
3f602fa
Add rules to documentation
geerteltink Sep 6, 2018
39ba67a
Add some test cases
geerteltink Sep 6, 2018
e9d9a2d
Update docs for alias functions
geerteltink Sep 6, 2018
2d29543
Document the new way to ignore parts of a file phpcs:enable phpcs:dis…
geerteltink Sep 6, 2018
2397775
Don't allow fully qualified classes from global space
geerteltink Sep 6, 2018
a063051
Add useful tests
geerteltink Sep 6, 2018
29b05a0
Fix typo in ci script
geerteltink Sep 6, 2018
d35d34e
Don't remove xdebug as php nightly doesn't have it
geerteltink Sep 6, 2018
b4d915d
Test expected violations for fixable code
geerteltink Sep 6, 2018
45d14bd
Require ? when default value is null
geerteltink Sep 6, 2018
5b00974
Improve documentation formatting
geerteltink Sep 6, 2018
dc11e2e
Add PSR-12 test cases
geerteltink Sep 6, 2018
6dc1801
Update concatenation spacing test
geerteltink Sep 6, 2018
c7718be
Update expected phpcs report
geerteltink Sep 6, 2018
7367ecd
Improve concatenation tests
geerteltink Sep 7, 2018
a88527c
Add phpcs cache for speed improvement
geerteltink Sep 7, 2018
c15e28e
Document the coding standard and all rules
geerteltink Sep 7, 2018
d044763
First set of documentation improvements and fixes
geerteltink Sep 7, 2018
f722edd
More grammar fixes
geerteltink Sep 8, 2018
ba703a4
Reorder rules and disable double rules
geerteltink Sep 8, 2018
2702288
Add missing rule: Class name resolution via ::class
geerteltink Sep 8, 2018
b9509c8
Add coding-style-guide to mkdocs config
geerteltink Sep 8, 2018
4da5772
Add changelog for #5
geerteltink Sep 8, 2018
9737c95
Improve wording and add example for Generic.Classes.DuplicateClassName
geerteltink Sep 8, 2018
7ea79cc
It's raining outside, so more examples and improved formatting
geerteltink Sep 8, 2018
70b63af
Update phpcs config in readme
geerteltink Sep 8, 2018
f93740b
Update array indentation test
geerteltink Sep 9, 2018
c531490
Don't align equal statements, double arrow arrays or phpdoc comment tags
geerteltink Sep 11, 2018
aba1c3a
Remove early exit rule as it is overly aggressive when "fixing" the code
geerteltink Sep 11, 2018
a276b18
Add references
geerteltink Sep 11, 2018
bf0d951
Update changelog with latest changes
geerteltink Sep 11, 2018
3386b02
Update intro and readme
geerteltink Sep 11, 2018
300e613
Fix creation of double trailing commas on specific arrays
geerteltink Sep 11, 2018
61f3bc5
PSR-12: Force Unix LF (linefeed) line ending
geerteltink Sep 11, 2018
ea3de20
Allow global functions
geerteltink Sep 11, 2018
53a7dcd
Align comments, statements and arrays
geerteltink Sep 19, 2018
850f481
Align array double arrows
geerteltink Sep 19, 2018
5eaa798
Validate fixes before running tests
geerteltink Sep 19, 2018
41d35b9
Fix single arrow indention test
geerteltink Sep 19, 2018
3974380
Reset array alignment and start from scratch
geerteltink Sep 20, 2018
137dc8a
Update slevomat/coding-standard requirement and add regression test for
geerteltink Sep 20, 2018
61f6b4b
Add test for constant aligning
geerteltink Sep 20, 2018
5f2280d
Align double arrows in multiline arrays
geerteltink Sep 21, 2018
a3d45d7
Update expected errors report
geerteltink Sep 21, 2018
27b735f
Require php_codesniffer 3.3.2
geerteltink Oct 6, 2018
95213b5
Update array indentation tests
geerteltink Oct 6, 2018
38a2dea
Use new format to declare array properties
geerteltink Oct 6, 2018
7dc2f78
Update phpcs report
geerteltink Oct 6, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.phpcs-cache
/clover.xml
/composer.lock
/coveralls-upload.json
/docs/html/
/mkdocs.site.yml
/phpcs.log
/phpcs.xml
/phpunit.xml
/test/fix/
/vendor/
/zf-mkdoc-theme.tgz
/zf-mkdoc-theme/
45 changes: 45 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
sudo: false
language: php

php:
- 7.1
- 7.2
- nightly

cache:
directories:
- $HOME/.composer/cache

before_install:
- composer self-update

install: travis_retry composer update --prefer-dist

script:
- vendor/bin/phpcs
- vendor/bin/phpcs $(find test/fixable/* | sort) --report=summary --report-file=phpcs.log; diff test/expected-report.txt phpcs.log

stages:
- Validate against schema
- Test
- Validate fixes

jobs:
allow_failures:
- php: nightly

include:
- stage: Validate against schema
addons:
apt:
packages:
- libxml2-utils
script: xmllint --noout --schema vendor/squizlabs/php_codesniffer/phpcs.xsd src/ZendCodingStandard/ruleset.xml

- stage: Validate fixes
before_script:
- cp -R test/fixable/ test/fix/
script: vendor/bin/phpcbf test/fix; diff test/fix test/fixed

notifications:
email: false
138 changes: 138 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,144 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 2.0.0 - TBD

### Added

- [#5](https://github.com/zendframework/zend-coding-standard/pull/5) adds
online documentation: https://docs.zendframework.com/zend-coding-standard/
- [#5](https://github.com/zendframework/zend-coding-standard/pull/5) adds
PSR-12 rules.

*NOTE:* PSR-12 is not finalized. e.g. The `!` operator and `:` placement for
return values are still under discussion. We will change these rules, and,
when PSR-12 is finalized, adapt them.
- [#5](https://github.com/zendframework/zend-coding-standard/pull/5) extends
PSR-12 with ZendFramework specific rules:

*NOTE:* Most of these rules should look familiar as they are already being
used in components rewritten for PHP 7.1.

- There should be one space on either side of an equals sign used to assign
a value to a variable. In case of a block of related assignments, more
space may be inserted before the equal sign to promote readability.
- Short array syntax must be used to define arrays.
- All values in multiline arrays must be indented with 4 spaces.
- All array values must be followed by a comma, including the last value.
- Whitespace is not allowed around the opening bracket or before the
closing bracket when referencing an array.
- All double arrow symbols must be aligned to one space after the longest
array key.
- The `global` keyword may not be used.
- The `PHP_SAPI` constant must be used instead of the `php_sapi_name()`
function.
- PHP function calls must be in lowercase.
- PHP functions which are an alias may not be used.
- Deprecated functions should not be used.
- Comments may be omitted and should not be used for typehinted arguments.
- Comments may not start with `#`.
- Comments may not be empty.
- The words _private_, _protected_, _static_, _constructor_, _deconstructor_,
_Created by_, _getter_ and _setter_, may not be used in comments.
- The annotations `@api`, `@author`, `@category`, `@created`, `@package`,
`@subpackage` and `@version` may not be used in comments. Git commits
provide accurate information.
- The asterisks in a doc comment should align, and there should be one space
between the asterisk and tag.
- If a function throws any exceptions, they should be documented in `@throws`
tags.
- The `@var` tag may be used in inline comments to document the _Type_ of
properties.
- Comments with single-line content should be written as one-liners.
- Shorthand scalar typehint variants must be used in docblocks.
- Each PHP file should have a page level docblock with `@see`, `@copyright`
and `@license`. The copyright date should only be adjusted if the file has
changed.
- Each PHP file should have a strict type declaration at the top after the
page level docblock.
- Import statements should be alphabetically sorted.
- Import statements should not be grouped.
- Each import statement should be on its own line.
- Absolute class name references, functions and constants should be imported.
- Unused import statements are not allowed.
- Classes and function within the same namespace should not be imported.
- Imports should not have an alias with the same name.
- A class should not have unused private constants, (or write-only)
properties and methods.
- Class name resolution via `::class` should be used instead of
`__CLASS__`, `get_class()`, `get_class($this)`, `get_called_class()` and
`get_parent_class()`.
- Methods may not have the final declaration in classes declared as final.
- The colon used with return type declarations MUST be surrounded with 1
space.
- Nullable and optional arguments, which are marked as `= null`, must have
the `?` symbol present.
- Control Structures must have at least one statement inside of the body.
- Catch blocks may be empty.
- Catch blocks must be reachable.
- Catch blocks must use `Throwable` instead of `Exception`.
- The not (`!`) operator must be followed by exactly one space.
- The `&&` and `||` operators must be used instead of `AND` and `OR`.
- The null coalescing operator `??` should be used when possible.
- Assignment operators (eg `+=`, `.=`) should be used when possible.
- Unused variables should not be passed to closures via `use`.
- The code may not contain unreachable code.
- The backtick operator may not be used for execution of shell commands.
- Class and Interface names should be unique in a project and must have a
unique fully qualified name.
- Methods that only call the parent method should not be defined.
- Files that contain PHP code should only have PHP code and should not have
any _"inline HTML"_.
- There must be exactly one space after a type cast.
- Constructors should be named `__construct`, not after the class.
- The opening PHP tag should be the first item in the file.
- Strings should not be concatenated together unless used in multiline for
readability.
- Loose `==` and `!=` comparison operators should not be used. Use `===`
and `!==` instead.
- An early exit strategy should be used where possible to reduce the level
of control structures.
- Language constructs must be used without parentheses where possible.
- Short list syntax `[...]` should be used instead of `list(...)`.
- Short form of type keywords must be used. i.e. `bool` instead of
`boolean`, `int` instead of `integer`, etc. The `binary` and `unset` cast
operators are not allowed.
- Parentheses should not be used if they can be omitted.
- Semicolons `;` should not be used if they can be omitted.
- Variables should be returned directly instead of assigned to a variable
which is not used.
- The `self` keyword should be used instead of the current class name, and
should not have spaces around `::`.
- Global functions should not be used.
- Static methods should not use `$this`.
- Double quote strings may only be used if they contain variables.
- Strings should not be enclosed in parentheses when being echoed.
- Type casts should not have whitespace inside the parentheses.
- The opening brace for functions should be on a new line with no blank
lines surrounding it.
- The PHP constructs `echo`, `print`, `return`, `include`, `include_once`,
`require`, `require_once`, and `new`, should have one space after them.
- The object operator `->` should not have any spaces around it.
- Semicolons should not have spaces before them.
- The code should not contain superfluous whitespaces. e.g. multiple empty
lines, trailing spaces, etc.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 1.0.0 - 2016-11-09

Initial public release. Incorporates rules for:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016, Zend Technologies USA, Inc.
Copyright (c) 2016-2018, Zend Technologies USA, Inc.

All rights reserved.

Expand Down
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
Zend Framework Coding Standard
==============================
# zend-coding-standard

Repository with all coding standard ruleset for Zend Framework repositories.
[![Build Status](https://secure.travis-ci.org/zendframework/zend-coding-standard.svg?branch=master)](https://secure.travis-ci.org/zendframework/zend-coding-standard)

The coding standard ruleset for Zend Framework components.

Installation
------------
## Installation

1. Install the module via composer by running:

Expand All @@ -26,22 +25,32 @@ Installation

```xml
<?xml version="1.0"?>
<ruleset name="Zend Framework Coding Standard">
<rule ref="./vendor/zendframework/zend-coding-standard/ruleset.xml"/>

<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>

<!-- Show progress -->
<arg value="p"/>

<!-- Paths to check -->
<file>config</file>
<file>src</file>
<file>test</file>

<!-- Include all rules from the Zend Coding Standard -->
<rule ref="ZendCodingStandard"/>
</ruleset>
```

You can add or exclude some locations in that file.
For a reference please see: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml


Usage
-----
## Usage

* To run checks only:

Expand All @@ -50,7 +59,7 @@ Usage
```

* To automatically fix many CS issues:

```bash
$ composer cs-fix
```
43 changes: 41 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
{
"name": "zendframework/zend-coding-standard",
"description": "Zend Framework coding standard",
"type": "phpcodesniffer-standard",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm so happy that composer has made things like plugin registration automated via package type!

"description": "Zend Framework Coding Standard",
"license": "BSD-3-Clause",
"keywords": [
"zf",
"zendframework",
"coding standard"
],
"support": {
"docs": "https://docs.zendframework.com/zend-coding-standard/",
"issues": "https://github.com/zendframework/zend-coding-standard/issues",
"source": "https://github.com/zendframework/zend-coding-standard",
"rss": "https://github.com/zendframework/zend-coding-standard/releases.atom",
"chat": "https://zendframework-slack.herokuapp.com",
"forum": "https://discourse.zendframework.com/c/questions/components"
},
"require": {
"squizlabs/php_codesniffer": "^2.7"
"php": "^7.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.4",
"slevomat/coding-standard": "^4.7.3",
"squizlabs/php_codesniffer": "^3.3.1"
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-4": {
"ZendCodingStandard\\": "src/ZendCodingStandard"
}
},
"autoload-dev": {
"psr-4": {
"ZendCodingStandardTest\\": "test/"
}
},
"scripts": {
"check": [
"@test-prepare",
"@test-fix",
"@test-diff"
],
"create-report": "phpcs $(find test/fixable/* | sort) --report=summary --report-file=test/expected-report.txt",
"test-prepare": "rm -rf test/fix/; cp -R test/fixable/ test/fix/;",
"test-fix": "phpcbf test/fix > /dev/null || true",
"test-diff": "diff test/fix test/fixed"
}
}
2 changes: 1 addition & 1 deletion CONDUCT.md → docs/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributor Code of Conduct

The Zend Framework project adheres to [The Code Manifesto](http://codemanifesto.com)
This project adheres to [The Code Manifesto](http://codemanifesto.com)
as its guidelines for contributor interactions.

## The Code Manifesto
Expand Down
Loading