yiisoft / yii2-coding-standards

Yii 2 coding standards
181 stars 49 forks source link

How to install Yii2 coding standard #23

Closed bubifengyun closed 8 years ago

bubifengyun commented 8 years ago

/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.

larryli commented 8 years ago

You can add yiisoft/yii2-coding-standards on composer.json:

    "require-dev": {
        "yiisoft/yii2-debug": "*",
        "yiisoft/yii2-gii": "*",
        "yiisoft/yii2-coding-standards": "2.*"
    }

Next, create a phpcs.xml.dist file on the project root directory, see PHP_CodeSniffer Documentation: Using a Default Configuration File

<?xml version="1.0"?>
<ruleset name="Yii2 App Basic Standard">
    <description>Yii2 App Basic coding standard</description>

    <exclude-pattern>/runtime/*</exclude-pattern>
    <exclude-pattern>/web/assets/*</exclude-pattern>
    <exclude-pattern>/vendor/*</exclude-pattern>
    <exclude-pattern>/requirements.php</exclude-pattern>

    <rule ref="vendor/yiisoft/yii2-coding-standards/Yii2"/>

    <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace">
        <exclude-pattern>/migrations/*</exclude-pattern>
    </rule>
    <rule ref="Squiz.Classes.ValidClassName.NotCamelCaps">
        <exclude-pattern>/migrations/*</exclude-pattern>
    </rule>
</ruleset>

Last, use command:

php composer.phar install --dev
vendor/bin/phpcs --encoding=utf-8 --extensions=php .
bubifengyun commented 8 years ago

Thank you, i had solved this problem. I used XAMPP, and set

sudo ln -s /opt/lampp/bin/php /usr/local/bin/php
sudo ln -s /opt/lampp/bin/pear /usr/local/bin/pear

installed the PHPCodeSniffer, and find the folder /opt/lampp/lib/php/PHP/CodeSniffer/Standards/_

So, I copy Yii2 folder from this project to the above folder. That is

/opt/lampp/lib/php/PHP/CodeSniffer/Standards
  Yii2/{ruleset.xml,Sniffs}
  PSR2
  <many standards>

Then, Yii2 can be used like PSR2 and Zend. Thank you.