phpstan 浣跨敤鎸囧崡

https://medium.com/@ondrejmirtes
phpstan
姊崇悊淇鎵惧嚭甯歌浠g爜闂锛岃緭鍑轰竴涓帴鍏ユ枃妗o紝寮涓涓垎浜細涓庡悓浜嬪皬浼欎即鍒嗕韩浜ゆ祦锛屼娇浠g爜鏇寸鍚坧sr4瑙勮寖銆

  1. 瀹夎
    濡傛灉鏄瀯寤哄湪寮鍙戠幆澧冧緵鑷繁鑷祴锛岄偅涔
    composer require --dev phpstan/phpstan
    濡傛灉鏄瀯寤哄湪鐢熶骇鐜锛岄偅涔
    composer require phpstan/phpstan
    dockerfile鏂囦欢鍙兘鏄被浼艰繖鏍峰瓙鐨
FROM ***.cr.aliyuncs.com/kpos/hyperf-base:7.4

RUN apk add php7-pecl-xlswriter --repository http://nl.alpinelinux.org/alpine/edge/testing

WORKDIR /opt/www

COPY ./composer.* /opt/www/

RUN composer install --no-dev -o --prefer-dist \
    && composer clearcache \
    && composer dump-autoload -o

COPY . /opt/www

RUN composer analyse

RUN php bin/hyperf.php

ENTRYPOINT ["php", "/opt/www/bin/hyperf.php", "start"]
  "scripts": {
    "analyse": "phpstan analyse --memory-limit 1000M -c phpstan.neon",
  }
  1. 鍒涘缓涓涓猵hpstan.neon鏂囦欢,骞惰缃渶楂樼瓑绾9
parameters:
  level: 9
  paths:
      - src

2.1 寮鍚弗鏍兼ā寮
composer require phpstan/phpstan-strict-rules

includes:
    - vendor/phpstan/phpstan-strict-rules/rules.neon
  1. 蹇界暐鎶ラ敊
ignoreErrors:
      - '#with no value type specified in iterable type array.#'

reportUnmatchedIgnoredErrors

  1.     /**
         * @return Collection|Payment[]
         */
        public function getPayments(array $codes): Collection
        {
            return Payment::query()->whereIn('code_name', $codes)->get();
        }
    
    /**
     * @return Collection<Payment>
     */
    public function getPayments(array $codes): Collection
    {
        return Payment::query()->whereIn('code_name', $codes)->get();
    }
  1. Baseline

鏈夋椂鍊欑敤鐨刾hpstan鐗堟湰寰堟棫锛屽崌绾т箣鍚庡彂鐜颁簡鍑犵櫨鍑犲崈涓敊璇紝杩欎釜鏃跺欏彲浠ョ洿鎺ヤ娇鐢˙aseline,鏃т唬鐮佸氨绛変互鍚庢湁鏃堕棿鍐嶆敼锛屾柊鐨勪唬鐮佸氨灏嗛伒寰洿楂樼殑鏍囧噯鏉ュ紑鍙戙

vendor/bin/phpstan analyse --level 9 --configuration phpstan.neon src --generate-baseline

All posts

Other pages

鍙戣〃璇勮

閭鍦板潃涓嶄細琚叕寮銆 蹇呭~椤瑰凡鐢*鏍囨敞