{"id":15866701,"url":"https://github.com/wata727/phan-query-plugin","last_synced_at":"2025-04-01T21:22:20.005Z","repository":{"id":62546788,"uuid":"131739169","full_name":"wata727/phan-query-plugin","owner":"wata727","description":"A Phan plugin to add a new rule without writing plugins","archived":false,"fork":false,"pushed_at":"2018-05-05T13:12:28.000Z","size":33,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T08:13:07.836Z","etag":null,"topics":["lint","linter","phan","php","plugin","static-analysis"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wata727.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-01T16:58:45.000Z","updated_at":"2019-10-28T12:37:29.000Z","dependencies_parsed_at":"2022-11-02T22:15:59.104Z","dependency_job_id":null,"html_url":"https://github.com/wata727/phan-query-plugin","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wata727%2Fphan-query-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wata727%2Fphan-query-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wata727%2Fphan-query-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wata727%2Fphan-query-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wata727","download_url":"https://codeload.github.com/wata727/phan-query-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246710239,"owners_count":20821362,"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":["lint","linter","phan","php","plugin","static-analysis"],"created_at":"2024-10-05T23:21:57.454Z","updated_at":"2025-04-01T21:22:19.986Z","avatar_url":"https://github.com/wata727.png","language":"PHP","readme":"# Phan Query Plugin\n\n[![Build Status](https://travis-ci.org/wata727/phan-query-plugin.svg?branch=master)](https://travis-ci.org/wata727/phan-query-plugin)\n[![Latest Stable Version](https://poser.pugx.org/wata727/phan-query-plugin/v/stable)](https://packagist.org/packages/wata727/phan-query-plugin)\n[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)\n\nA Phan plugin to add a new rule without writing plugins. This plugin inspired by [Querly](https://github.com/soutaro/querly).\n\n## Installation\n\nYou can install this plugin with composer.\n\n```\n$ composer require --dev wata727/phan-query-plugin\n```\n\nAfter that, Set this plugin path to `.phan/config.php`.\n\n```php\n\u003c?php\n\nreturn [\n    \"plugins\" =\u003e [__DIR__.\"/../vendor/wata727/phan-query-plugin/plugins/QueryPlugin.php\"]\n];\n```\n\n## Quick Start\n\nAt first, creates files as follows:\n\n`.phan/config.php`:\n\n```php\n\u003c?php\n\nreturn [\n    \"plugins\" =\u003e [__DIR__.\"/../vendor/wata727/phan-query-plugin/plugins/QueryPlugin.php\"]\n];\n```\n\n`.phan/query.php`:\n\n```php\n\u003c?php\n\nreturn [\n    [\n        \"type\" =\u003e \"PhanQueryCatFound\",\n        \"message\" =\u003e \"Cat Found\",\n        \"pattern\" =\u003e '$cat-\u003emeow();',\n    ]\n];\n```\n\n`test.php`:\n\n```php\n\u003c?php\n\nnamespace Foo\\Bar;\n\nclass Cat\n{\n    public function meow()\n    {\n        echo \"Meow!\";\n    }\n}\n\n$cat = new Cat();\n$cat-\u003emeow();\n```\n\nTry following command:\n\n```\n$ vendor/bin/phan test.php\n```\n\nYou can get the result:\n\n```\ntest.php:12 PhanQueryCatFound Cat Found\n```\n\n### What happened?\n\nThis plugin loads `.phan/query.php` and searches for your code that matches the defined `pattern` of Query.\nIn the above example, since the code matching `$cat-\u003emeow();` existed in line 12, it was emitted as Phan's issue.\n\nThe emitted issue has type and message as well as Phan's original issue. These are `type` and `message` defined in your Query.\n\n### What is the difference with regular expressions?\n\nThis plugin uses AST for matching node. For example, `$cat-\u003emeow($arg1, $arg2);` matches all of the following:\n\n- `$cat-\u003emeow($arg1, $arg2);`\n- `$cat-\u003emeow($arg1 , $arg2);`\n- `$cat-\u003emeow($arg1,$arg2);`\n\n## Query Syntax\n\nWhat kind of syntax can be written in `pattern`? This is PHP-like pattern matching syntax, which satisfying the following:\n\n- The pattern expressions must be valid as PHP\n  - For example, a trailing semicolon is required.\n- But the `\u003cKlass\u003e` syntax is available in the pattern.\n\n### `\u003cKlass\u003e` Syntax\n\nYou can use `\u003cKlass\u003e` syntax in a pattern of Query. If you use it, the following pattern matches `test.php` like the above.\n\n```\n\u003c?php\n\nreturn [\n    [\n        \"type\" =\u003e \"PhanQueryAllCatFound\",\n        \"message\" =\u003e \"Cat Found\",\n        \"pattern\" =\u003e '\u003cFoo\\Bar\\Cat\u003e-\u003emeow();',\n    ]\n];\n```\n\nThis means that the Query checks types of variables when use this pattern. Also, if you specify `\u003cany\u003e`, it matches all variables.\n\n## Testing your Query\n\nYou can write a test to make sure the Query works correctly. For example:\n\n```php\n\u003c?php\n\nreturn [\n    [\n        \"type\" =\u003e \"PhanQueryCatFound\",\n        \"message\" =\u003e \"Cat Found\",\n        \"pattern\" =\u003e '$cat-\u003emeow();',\n        \"test\" =\u003e [\n            \"match\" =\u003e \u003c\u003c\u003c'EOD'\n\u003c?php\n\nnamespace Foo\\Bar;\n\nclass Cat\n{\n    public function meow()\n    {\n        echo \"Meow!\";\n    }\n}\n\n$cat = new Cat();\n$cat-\u003emeow();\nEOD\n            ,\n            \"unmatch\" =\u003e \u003c\u003c\u003c'EOD'\n\u003c?php\n\nnamespace Foo\\Bar;\n\nclass Cat\n{\n    public function meow()\n    {\n        echo \"Meow!\";\n    }\n}\n\n$dog = new Cat();\n$dog-\u003emeow();\nEOD\n        ],\n    ],\n];\n```\n\nThe `match` value is the code that matches `pattern`, and the `unmatch` value is the code that not match `pattern`. You can run the tests with the following command:\n\n```\n$ vendor/bin/phan_query_test\nAll 2 tests are passed!\n```\n\n## Author\n\n[Kazuma Watanabe](https://github.com/wata727)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwata727%2Fphan-query-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwata727%2Fphan-query-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwata727%2Fphan-query-plugin/lists"}