Git Product home page Git Product logo

cakephp-generation-gap-pattern-model-baker's Issues

存在しないテーブルのリレーションコードを出力しない

users.store_id などの id カラムが存在すると、関連テーブルがあると判断して Model/Table にリレーションコードを出力する
これは bake の仕様であり、継承している本プラグインも同じ動作をするが、存在しないテーブルはコードを出力しないようにできるのなら対応したい

生成されるファイルにphpcbfでフォーマッタをかけなくても良いようにしたい

cakeのマイナーバージョンアップに伴いコーディング規約が変更された。
現状、bake extended_modelコマンドで生成されるコードのフォーマットと乖離があるためbaked/配下を含む生成されたコードに対してphpcbfを実行してやる必要があるためこちらを修正したい。

paymentsのModelを作成した時

bash-4.2# bin/cake bake extended_model payments

以下4つのファイルにフォーマッタ(phpcbf)を適用してやる必要がある

$ git add .
$ docker run --rm -v `pwd -W`:/app isanasan/phpcs phpcbf --standard=.circleci/phpcs.xml `git diff --cached --name-only`

PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
/app/src/Model/Baked/Entity/Payment.php               1      0
/app/src/Model/Baked/Table/PaymentsTable.php          4      11
/app/src/Model/Entity/Payment.php                     1      0
/app/src/Model/Table/PaymentsTable.php                5      0
----------------------------------------------------------------------
A TOTAL OF 11 ERRORS WERE FIXED IN 4 FILES
----------------------------------------------------------------------

DateTime型のカラムにCarbonを代入するとPHPStanlエラーになる

下記のPHPStanエラーを未然に防ぐためにCake\I18n\FrozenTime|nullのtypehintをDateTimeInterfaceにしたい。

------ ----------------------------------------------------------------------------------------------------------------------------------------
  Line   OrganizationInvoiceGroup.php
 ------ ----------------------------------------------------------------------------------------------------------------------------------------
  18     Property App\Model\Baked\Entity\OrganizationInvoiceGroup::$deleted (Cake\I18n\FrozenTime|null) does not accept Carbon\CarbonImmutable.
 ------ ----------------------------------------------------------------------------------------------------------------------------------------

BaseEntity を継承する

何が起きていますか

Entity の共通処理を BaseEntity に書きたい

やりたいこと

Baked/Entity で BaseEntity を継承する

前提

BaseEntity は必ず Cake\ORM\Entity を継承していること

bake, CakePHP 本体のコード変更に追随する

課題

twig や Class など、vendor 配下からコピーしてきて継承しているクラスがある。
これらのファイルは、bake や CakePHP のバージョンアップでの変更に追随する必要がある。

起きていること

以下のような不都合が生じる。
#10

対応案

CI で 以下を検出できないか。

  • vendor 配下の対象 twig, class と、plugin のファイルの差分検知
  • 差分があれば CI を通さない

自動生成したコードに対するphpmdの指摘をデフォルトで無視したい

/home/circleci/lancers_admin/src/Model/Baked/Table/PaymentsTable.php:119	The method validationDefault() has 113 lines of code. Current threshold is set to 100. Avoid really long methods.)

カラム数が多いTableのModelで指摘されてしまう。
templates/bake/Model/extended_table.twigに対して下記の修正で良いと思う。

/**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
+    * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function validationDefault(Validator $validator): Validator
    {

`bake all` に対応する

何が起きていますか

bake all では依然 bake model が実行されているため、本プラグインが活用できないシーンがある

やりたいこと

bake all でも本プラグインを実行する
なお、実行したくない場合、オプション定義可能にできるといい

typehintの変更に追随する

EntityTrait::$_accessibleの型定義がarrayとなっているためテンプレートの型定義をそれに合わせて修正する。
詳しくはこちら

具体的な指摘内容

------ -------------------------------------------------------------------- 
  Line   Model/Baked/Entity/Payment.php                                      
 ------ -------------------------------------------------------------------- 
  70     PHPDoc type array of property                                       
         App\Model\Baked\Entity\Payment::$_accessible is not covariant with  
         PHPDoc type array<bool> of overridden property                      
         Cake\ORM\Entity::$_accessible.                                      
         💡 You can fix 3rd party PHPDoc types with stub files:               
            https://phpstan.org/user-guide/stub-files                        
 ------ -------------------------------------------------------------------- 

Model/Baked/Entity/Payment.phpファイル上ではここ

class Payment extends Entity
{
    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * Note that when '*' is set to true, this allows all unspecified fields to
     * be mass assigned. For security purposes, it is advised to set '*' to false
     * (or remove it), and explicitly make individual fields accessible as needed.
     *
     * @var array
     */
    protected $_accessible = [
                'user_id' => 1,

こうなってほしい

class Payment extends Entity
{
    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * Note that when '*' is set to true, this allows all unspecified fields to
     * be mass assigned. For security purposes, it is advised to set '*' to false
     * (or remove it), and explicitly make individual fields accessible as needed.
     *
-    * @var array
     */
    protected $_accessible = [
-            'user_id' => 1,
+           'user_id' => true,

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.