cakephp-generation-gap-pattern-model-baker's People
Forkers
zonuexecakephp-generation-gap-pattern-model-baker's Issues
テーブルのコメントを取得し、各 template の項目名に反映する
何が起きていますか
template の項目名がカラム名そのままのため、bake の都度日本語名を手入力している
やりたいこと
テーブル定義にコメントが適切に入力された前提で、コメントを template に反映し自動化する
bake, CakePHP 本体のコード変更に追随する
課題
twig や Class など、vendor 配下からコピーしてきて継承しているクラスがある。
これらのファイルは、bake や CakePHP のバージョンアップでの変更に追随する必要がある。
起きていること
以下のような不都合が生じる。
#10
対応案
CI で 以下を検出できないか。
- vendor 配下の対象 twig, class と、plugin のファイルの差分検知
- 差分があれば CI を通さない
BaseEntity を継承する
何が起きていますか
Entity の共通処理を BaseEntity に書きたい
やりたいこと
Baked/Entity で BaseEntity を継承する
前提
BaseEntity は必ず Cake\ORM\Entity
を継承していること
`bake all` に対応する
何が起きていますか
bake all
では依然 bake model
が実行されているため、本プラグインが活用できないシーンがある
やりたいこと
bake all
でも本プラグインを実行する
なお、実行したくない場合、オプション定義可能にできるといい
自動生成したコードに対する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
{
生成されるファイルに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
----------------------------------------------------------------------
Baked Model の function を protected にする
baked model は直接は使わず継承して使うので、関数は全部 protected とする。
存在しないテーブルのリレーションコードを出力しない
users.store_id などの id カラムが存在すると、関連テーブルがあると判断して Model/Table にリレーションコードを出力する
これは bake の仕様であり、継承している本プラグインも同じ動作をするが、存在しないテーブルはコードを出力しないようにできるのなら対応したい
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,
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.
------ ----------------------------------------------------------------------------------------------------------------------------------------
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.