{"id":13684167,"url":"https://github.com/CakeDC/cakephp-phpstan","last_synced_at":"2025-04-30T20:33:16.332Z","repository":{"id":45255583,"uuid":"266729527","full_name":"CakeDC/cakephp-phpstan","owner":"CakeDC","description":"CakePHP extension for PHPStan","archived":false,"fork":false,"pushed_at":"2024-11-11T14:31:36.000Z","size":228,"stargazers_count":33,"open_issues_count":1,"forks_count":6,"subscribers_count":15,"default_branch":"3.next-cake5","last_synced_at":"2024-11-11T15:32:36.534Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CakeDC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-25T08:48:24.000Z","updated_at":"2024-11-11T14:32:07.000Z","dependencies_parsed_at":"2024-02-26T20:28:13.493Z","dependency_job_id":"5566c5cc-89b5-4b08-9e70-5938597ed800","html_url":"https://github.com/CakeDC/cakephp-phpstan","commit_stats":{"total_commits":30,"total_committers":4,"mean_commits":7.5,"dds":"0.19999999999999996","last_synced_commit":"850f4b2ab375c89962260096c924c11b56dbce00"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CakeDC%2Fcakephp-phpstan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CakeDC%2Fcakephp-phpstan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CakeDC%2Fcakephp-phpstan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CakeDC%2Fcakephp-phpstan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CakeDC","download_url":"https://codeload.github.com/CakeDC/cakephp-phpstan/tar.gz/refs/heads/3.next-cake5","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224132812,"owners_count":17261290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T14:00:30.354Z","updated_at":"2025-04-30T20:33:16.326Z","avatar_url":"https://github.com/CakeDC.png","language":"PHP","funding_links":[],"categories":["Code Analysis","Plugins"],"sub_categories":["Code Analysis"],"readme":"# CakePHP extension for PHPStan\n\n![Build Status](https://github.com/CakeDC/cakephp-phpstan/actions/workflows/ci.yml/badge.svg)\n[![Downloads](https://poser.pugx.org/CakeDC/cakephp-phpstan/d/total.svg)](https://packagist.org/packages/CakeDC/cakephp-phpstan)\n[![Latest Version](https://poser.pugx.org/CakeDC/cakephp-phpstan/v/stable.svg)](https://packagist.org/packages/CakeDC/cakephp-phpstan)\n[![License](https://poser.pugx.org/CakeDC/cakephp-phpstan/license.svg)](LICENSE.txt)\n\n* [PHPStan](https://phpstan.org/)\n* [CakePHP](https://cakephp.org/)\n\nProvide services and rules for a better PHPStan analyze on CakePHP applications, includes services to resolve types (Table, Helpers, Behaviors, etc)\nand multiple rules.\n\n# Installation\n\nTo use this extension, require it through [Composer](https://getcomposer.org/):\n\n```\ncomposer require --dev cakedc/cakephp-phpstan\n```\n\n\nIf you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer), then you're all set!\n\n\u003cdetails\u003e\n    \u003csummary\u003eManual installation\u003c/summary\u003e\n\nIf you don't want to use `phpstan/extension-installer`, include `extension.neon` in your project's PHPStan config:\n```\nincludes:\n    - vendor/cakedc/cakephp-phpstan/extension.neon\n```\n\n\u003c/details\u003e\n\n\n# General class load|fetch extensions\nFeatures included:\n1. Provide correct return type for `Cake\\ORM\\Locator\\LocatorInterface::get()`\n1. Provide correct return type for `Cake\\Controller\\Controller::loadComponent()`\n1. Provide correct return type for `Cake\\Controller\\Controller::fetchTable()`\n1. Provide correct return type for `Cake\\Controller\\Component::fetchTable()`\n1. Provide correct return type for `Cake\\Command\\Command::fetchTable()`\n1. Provide correct return type for `Cake\\Mailer\\Mailer::fetchTable()`\n1. Provide correct return type for `Cake\\View\\Cell::fetchTable()`\n1. Provide correct return type for `Cake\\Console\\ConsoleIo::helper()`\n\n# Table class return type extensions\n### TableEntityDynamicReturnTypeExtension\n1. Provide correct return type for `Cake\\ORM\\Table::get` based on your table class name\n1. Provide correct return type for `Cake\\ORM\\Table::newEntity` based on your table class name\n1. Provide correct return type for `Cake\\ORM\\Table::newEntities` based on your table class name\n1. Provide correct return type for `Cake\\ORM\\Table::newEmptyEntity` based on your table class name\n1. Provide correct return type for `Cake\\ORM\\Table::findOrCreate` based on your table class name\n\n\u003cdetails\u003e\n      \u003csummary\u003eExamples:\u003c/summary\u003e\n\n```php\n  //Now PHPStan know that \\App\\Models\\Table\\NotesTable::get returns \\App\\Model\\Entity\\Note\n  $note = $this-\u003eNotes-\u003eget(1);\n  $note-\u003enote = 'My new note';//No error\n\n  //Now PHPStan know that \\App\\Models\\Table\\NotesTable::newEntity returns \\App\\Model\\Entity\\Note\n  $note = $this-\u003eNotes-\u003enewEntity($data);\n  $note-\u003enote = 'My new note new entity';//No error\n\n  //Now PHPStan know that \\App\\Models\\Table\\NotesTable::newEmptyEntity returns \\App\\Model\\Entity\\Note\n  $note = $this-\u003eNotes-\u003enewEmptyEntity($data);\n  $note-\u003enote = 'My new note new empty entity';//No error\n\n   //Now PHPStan know that \\App\\Models\\Table\\NotesTable::findOrCreate returns \\App\\Model\\Entity\\Note\n  $note = $this-\u003eNotes-\u003efindOrCreate($data);\n  $note-\u003enote = 'My entity found or created';//No error\n\n  //Now PHPStan know that \\App\\Models\\Table\\NotesTable::newEntities returns \\App\\Model\\Entity\\Note[]\n  $notes = $this-\u003eNotes-\u003enewEntities($data);\n  foreach ($notes as $note) {\n    $note-\u003enote = 'My new note';//No error\n  }\n```\n\u003c/details\u003e\n\n### TableFirstArgIsTheReturnTypeExtension\n1. Provide correct return type for `Cake\\ORM\\Table::patchEntity` based on the first argument passed\n1. Provide correct return type for `Cake\\ORM\\Table::patchEntities` based on the first argument passed\n1. Provide correct return type for `Cake\\ORM\\Table::save` based on the first argument passed\n1. Provide correct return type for `Cake\\ORM\\Table::saveOrFail` based on the first argument passed\n1. Provide correct return type for `Cake\\ORM\\Table::saveMany` based on the first argument passed\n1. Provide correct return type for `Cake\\ORM\\Table::saveManyOrFail` based on the first argument passed\n1. Provide correct return type for `Cake\\ORM\\Table::deleteMany` based on the first argument passed\n1. Provide correct return type for `Cake\\ORM\\Table::deleteManyOrFail` based on the first argument passed\n1. Provide correct return type for `Cake\\ORM\\Locator\\LocatorAwareTrait::fetchTable` based on the first argument passed\n1. Provide correct return type for `Cake\\Mailer\\MailerAwareTrait::getMailer` based on the first argument passed\n\n\u003cdetails\u003e\n      \u003csummary\u003eExamples:\u003c/summary\u003e\n\n```php\n  //Now PHPStan know that \\App\\Models\\Table\\NotesTable::get returns \\App\\Model\\Entity\\Note\n  $note = $this-\u003eNotes-\u003eget(1);\n  $notes = $this-\u003eNotes-\u003enewEntities($data);\n\n  //Since PHPStan knows the type of $note, these methods call use the same type as return type:\n  $note = $this-\u003eNotes-\u003epatchEntity($note, $data);\n  $text = $note-\u003enote;//No error.\n\n  $note = $this-\u003eNotes-\u003esave($note);\n  $text = $note-\u003enote;//No error.\n\n  $note = $this-\u003eNotes-\u003esaveOrFail($note);\n  $text = $note-\u003enote;//No error.\n  //Since PHPStan knows the type of $notes, these methods call use the same type as return type:\n  $notes = $this-\u003eNotes-\u003epatchEntities($notes);\n  $notes = $this-\u003eNotes-\u003esaveMany($notes);\n  $notes = $this-\u003eNotes-\u003esaveManyOrFail($notes);\n  $notes = $this-\u003eNotes-\u003edeleteMany($notes);\n  $notes = $this-\u003eNotes-\u003edeleteManyOrFail($notes);\n```\n\u003c/details\u003e\n\n# Rules\nAll rules provided by this library are included in [rules.neon](rules.neon) and are enabled by default:\n\n### AddAssociationExistsTableClassRule\nThis rule check if the target association has a valid table class when calling to Table::belongsTo,\nTable::hasMany, Table::belongsToMany, Table::hasOne and AssociationCollection::load.\n\n### AddAssociationMatchOptionsTypesRule\nThis rule check if association options are valid option types based on what each class expects. This cover calls to Table::belongsTo,\nTable::hasMany, Table::belongsToMany, Table::hasOne and AssociationCollection::load.\n\n### AddBehaviorExistsClassRule\nThis rule check if the target behavior has a valid class when calling to Table::addBehavior and BehaviorRegistry::load.\n\n### DisallowEntityArrayAccessRule\nThis rule disallow array access to entity in favor of object notation, is easier to detect a wrong property and to refactor code.\n\n### GetMailerExistsClassRule\nThis rule check if the target mailer is a valid class when calling to Cake\\Mailer\\MailerAwareTrait::getMailer.\n\n### LoadComponentExistsClassRule\nThis rule check if the target component has a valid class when calling to Controller::loadComponent and ComponentRegistry::load.\n\n### OrmSelectQueryFindMatchOptionsTypesRule\nThis rule check if the options (args) passed to Table::find and SelectQuery are valid find options types.\n\n### TableGetMatchOptionsTypesRule\nThis rule check if the options (args) passed to Table::get are valid find options types.\n\nTo enable this rule update your phpstan.neon with:\n\n```\nparameters:\n\tcakeDC:\n\t \tdisallowEntityArrayAccessRule: true\n```\n\n### How to disable a rule\nEach rule has a parameter in cakeDC 'namespace' to enable or disable, it is the same name of the\nrule with first letter in lowercase.\nFor example to disable the rule AddAssociationExistsTableClassRule you should have\n```\nparameters:\n\tcakeDC:\n\t \taddAssociationExistsTableClassRule: false\n```\n\n# PHPDoc Extensions\n### TableAssociationTypeNodeResolverExtension\nFix intersection association phpDoc to correct generic object type, ex:\n\nChange `\\Cake\\ORM\\Association\\BelongsTo\u0026\\App\\Model\\Table\\UsersTable` to `\\Cake\\ORM\\Association\\BelongsTo\u003c\\App\\Model\\Table\\UsersTable\u003e`\n\n\n### Tips\nTo make your life easier make sure to have `@mixin` and `@method` annotations in your table classes.\nThe `@mixin` annotation will help phpstan know you are using methods from behavior, and `@method` annotations\nwill allow it to know the correct return types for methods like `Table::get()`, `Table::newEntity()`.\n\nYou can easily update annotations with the plugin [IdeHelper](https://github.com/dereuromark/cakephp-ide-helper).\n\nSupport\n-------\n\nFor bugs and feature requests, please use the [issues](https://github.com/CakeDC/cakephp-phpstan/issues) section of this repository.\n\nCommercial support is also available, [contact us](https://www.cakedc.com/contact) for more information.\n\nContributing\n------------\n\nIf you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our [Contribution Guidelines](https://www.cakedc.com/contribution-guidelines) for detailed instructions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCakeDC%2Fcakephp-phpstan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCakeDC%2Fcakephp-phpstan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCakeDC%2Fcakephp-phpstan/lists"}