LoginSignup
17
14

More than 5 years have passed since last update.

PHP + Visual Studio Codeの開発環境を作る

Last updated at Posted at 2018-04-07

入れるもの

  • PHP Extension Pack
    • 以下が入る
      • PHP IntelliSense
      • PHP Debug
  • PHP DocBlocker
  • phpcs
  • php cs fixer
  • PHP Mess Detector
  • PHP Namespace Resolver

メモ

PHP IntelliSense

PHP7+じゃないとダメです
※デフォルトPHP7の環境なら特に何もしなくても大丈夫

PHP7以外の環境の場合、Visual Studio Codeのユーザー設定にPHP7のパスを設定する
自分の環境はphpenv使ってるので以下を設定しました

"php.executablePath": "/Users/xxxxx/.phpenv/versions/7.1.10/bin/php",

phpcs

phpcsが入ってないとダメなのでインストール
PHP_CodeSnifferに書いてあるので好きな方法で

今回はcomposerでインストールする

$ composer global require "squizlabs/php_codesniffer=*"

.bash_profileでcomposerでインストールされるパスを通しておきます
VSCodeの個人設定でも設定できるのでココはご自由に

export $HOME/.composer/vendor/bin

ユーザー設定には以下を追加しました

"phpcs.standard": "PSR2",

// 上でパスを通してない場合のみ、パスを通してれば不要
"phpcs.executablePath": "/Users/xxxxx/.composer/vendor/bin/phpcs",

php cs fixer

php-cs-fixerが入ってないと動かない
MacユーザーはbrewでインストールすればOK

$ brew upgrade php-cs-fixer

PHP Mess Detector

デフォルトだと結構うるさいのでルール設定したほうが良いです。

個人設定には自分ルールのXMLのパスを設定すればOKです

"phpmd.rules": "/path/to/phpmd_ruleset.xml",

ruleset

細かい作り方はDocumentationを参照

↓↓適当に作ったルール↓↓

phpmd_ruleset.xml
<?xml version="1.0"?>
<ruleset name="My first PHPMD rule set"
         xmlns="http://pmd.sf.net/ruleset/1.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
                     http://pmd.sf.net/ruleset_xml_schema.xsd"
         xsi:noNamespaceSchemaLocation="
                     http://pmd.sf.net/ruleset_xml_schema.xsd">
    <description>
        My custom rule set that checks my code...
    </description>

    <rule ref="rulesets/cleancode.xml">
        <exclude name="StaticAccess" />
    </rule>

    <rule ref="rulesets/codesize.xml" />
    <rule ref="rulesets/controversial.xml" />
    <rule ref="rulesets/design.xml" />
    <rule ref="rulesets/naming.xml">
        <properties>
            <property name="maximum" value="30" />
        </properties>
    </rule>
    <rule ref="rulesets/unusedcode.xml" />
</ruleset>
17
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
14