Giter Club home page Giter Club logo

yii2-coding-standards's Introduction

Yii 2 Web Framework Coding Standard

This repository contains settings for Yii2 coding style for various tools.

Getting code

You can get code style definition using one of the following methods.

  • Clone yiisoft/yii2-coding-standards repository:
$ git clone git://github.com/yiisoft/yii2-coding-standards.git
  • Install composer.phar distribution:
$ curl -sS https://getcomposer.org/installer | php

Or if your system doesn't have CURL installed:

$ php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
  • Set up all dependencies declared in composer.json:
$ php composer.phar install

PHP_Codesniffer

PHP_CodeSniffer coding standard, rule set and sniff token parsing classes for the Yii 2 Web Framework. Overally rules are based on PSR-12 standard with some minor exceptions and changes. Rules derived from PSR-12 standard and excluded in Yii2 standard were implemented (or planned to be) as sniff classes.

Rules could also be used for checking code style of an existing Yii2 applications.

Everything that is merged into main Yii2 development repository being checked with these rule set as well.

Using code style

After CodeSniffer is installed you can launch it with custom code style using the following syntax:

$ ./vendor/bin/phpcs --extensions=php --standard=Yii2 /home/resurtm/work/Yii2MegaApp/

Installation can be also be done automatically with this tool:

composer require --dev dealerdirect/phpcodesniffer-composer-installer

When using Composer 2.2 or higher, Composer will ask for your permission to allow this plugin to execute code. For this plugin to be functional, permission needs to be granted.

When permission has been granted, the following snippet will automatically be added to your composer.json file by Composer:

{
    "config": {
        "allow-plugins": {
            "dealerdirect/phpcodesniffer-composer-installer": true
        }
    }
}

When using Composer < 2.2, you can add the permission flag ahead of the upgrade to Composer 2.2, by running:

composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true

If you're using PhpStorm you can configure it to use CodeSniffer using Settings → PHP → Code Sniffer. Yii2 code style can be specified at Inspections → PHP → PHP Code Sniffer validation.

Useful links

PhpStorm

Yii uses PSR-12 as code style standard. You can choose these via SettingsCode StylePHPSet from...Predefined StylePSR12.

ADDITIONAL NOTES

Feel free to request additional features, submit bugs and problems.

Thank you for choosing Yii Framework!

yii2-coding-standards's People

Contributors

alexgx avatar arhell avatar bizley avatar cebe avatar edgardmessias avatar frenck avatar gsherwood avatar krukru avatar nkovacs avatar qiangxue avatar razvanphp avatar resurtm avatar samdark avatar schmunk42 avatar tanakahisateru avatar wintersilence avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-coding-standards's Issues

Undefined offset: 3 with PHP CodeSniffer 1.5.2

When running phpcs 1.5.2 with the Yii2 standard, the following error occurs:

PHP Notice: Undefined offset: 3 in CodeSniffer.php on line 1050

This is because even though the PrivatePropertiesUnderscoreSniff.php file is not listed in the ruleset, it's still being loaded. However it should be in a subfolder, for example Yii2/Sniffs/Classes. Then the class should be renamed to Yii2_Sniffs_Classes_PrivatePropertiesUnderscoreSniff.

PSR-12 standard release

Since Yii2 mostly follows the new extended rules, can we have a new release tag that enforces this standard?

It would help the community and junior developers follow the rules.

Thank you!
R

Add Alphabetical sorting of use statements

Something that I do a lot in my projects is having my use statements sorted lexicographically (Uppercase first, lowercase then).
Please have a look at some sniffs that already implement that feature here and here.

php-cs vs php-cs-fixer

Hi, what are you using to format the code, php-cs-fixer or php-cs, in the repository of the core yii2 are the rules for php-cs-fixer, which is the one currently used,

Thks,

How to install Yii2 coding standard

/opt/lampp/htdocs/www/wuzhishan$ /opt/lampp/bin/phpcs --extensions=php --standard=Yii2 ./
ERROR: the "Yii2" coding standard is not installed. The installed coding standards are PHPCS, Zend, Squiz, MySource, PSR2, PEAR and PSR1

I am so sorry that YII 2 coding standard is not installed. I cannot understand the Getting code.
I installed phpcs in my /opt/lampp/bin/phpcs.
my network is located in /opt/lampp/htdocs/www/wuzhishan/

this code from README.md

$ ./vendor/bin/phpcs --extensions=php --standard=Yii2 /home/resurtm/work/Yii2MegaApp/

what the last Yii2MegaApp means? is it an app?
Thank you.

Line indented incorrectly in rules

This code not valid for phpcs:

<?php
namespace test;

class NEW1
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['date'], 'date'],
            [
                ['ip_manual'],
                'filter',
                'filter' => function ($v) {
                        if ($this->operator == 3 || $this->operator == 4) { //here
                            return null;
                        }
                        return $v;
                    } //and here
            ],
        ];
    }
}

PHPCS requires ugly operations from me:

<?php
namespace test;

class NEW1
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['date'], 'date'],
            [
                ['ip_manual'],
                'filter',
                'filter' => function ($v) {
            if ($this->operator == 3 || $this->operator == 4) {
                return null;
            }
            return $v;
                }
            ],
        ];
    }
}

Why use the Coding-sandards extension

hello,
i came across your extension for the Yii coding standards and while going through the Yii-coding standards Docs i dont find anything that is not already covered by PhpCs, or maybe i am overlooking it.

The purpose of asking is that if i already am using the phpcs with PSR2 would i need to use this extension if i am working in Yii2 and how can it benefit me.

Coding Standards Fixer for Yii 2

You're probably aware of php-cs-fixer. Pretty useful and mature coding standards fixer.

It already includes lots of fixers (see the rules list) but Yii 2 has its specifics (especially PHPDocs)

What I did

  • I thought the Yii framework (and even 3rd party extensions whose want to stay up-to-date with the CS) can benefit from the work I have done (it's always cool when all code — yours and contributed — can be formatted in one command and you can even change some CS things fast and safely)
  • I wrote several fixers and a config builder and tested everything on the Yii 2 framework code (I tried to follow the Yii 2 CS as much as I could)
  • I'm ready to polish and tweak it to team's needs (and contribute/support it — although a code is not hard to understand)

Possible usage

Let's assume we've pushed several files to yii2-cs-fixer — its composer.json is basically dependency-free (I wrote fixers for php-cs-fixer 2.0):

"require-dev": {
    "friendsofphp/php-cs-fixer": "^2.0"
},

Then a project wishes to use our fixer will need:

  • run composer require --dev yiisoft/yii2-cs-fixer
  • place .php_cs.dist with this:
<?php

require_once __DIR__ . '/vendor/autoload.php';

return yii2\csfixer\Config::create()
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->in([
                // customizable
                __DIR__ . '/src',
                __DIR__ . '/tests',
                __DIR__ . '/yii2/framework',
            ])
            // customizable
            ->exclude('views')
    );
  • run php-cs-fixer fix to fix it (or php-cs-fixer fix --dry-run --diff to preview changes)

Summary

If you also think Yii 2 can benefit from this, I'm up to make it real — I will publish the source (it's not a prime-time ready yet — basically it misses docs) and tweak it to Yii 2 team needs.

In case you have questions or interested, I'm ready. If not, just close the issue. :)

There's a demo repository where I have applied fixes from my current configuration: https://github.com/sergeymakinen/yii2/commit/181e9e49d354a822d0add52062d4919bc2824b45

Indentation fixing

PHP_CodeSniffer config treats this code as valid

if (true) {
        // Note two tabs instead of one
        echo 'Hi there.', PHP_EOL;
}

Is there any way to fix this in CodeSniffer config?

Add tag to repository

Would be nice, if this repo could also introduce a version tag.
Otherwise you have to specify a development version in composer like @dev or dev-master.

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.