{"id":13824709,"url":"https://github.com/quasilyte/phpgrep","last_synced_at":"2025-10-24T01:10:51.601Z","repository":{"id":35934497,"uuid":"194259712","full_name":"quasilyte/phpgrep","owner":"quasilyte","description":"Syntax-aware grep for PHP code.","archived":false,"fork":false,"pushed_at":"2024-04-29T17:39:17.000Z","size":595,"stargazers_count":236,"open_issues_count":28,"forks_count":10,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-20T08:02:28.438Z","etag":null,"topics":["ast","code","code-search","go","golang","grep","php","php-grep","phpgrep","search","structural-search","structured-search","syntax"],"latest_commit_sha":null,"homepage":"","language":"Go","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/quasilyte.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-28T11:06:03.000Z","updated_at":"2025-05-15T07:47:04.000Z","dependencies_parsed_at":"2024-06-20T02:58:30.740Z","dependency_job_id":null,"html_url":"https://github.com/quasilyte/phpgrep","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/quasilyte/phpgrep","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fphpgrep","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fphpgrep/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fphpgrep/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fphpgrep/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quasilyte","download_url":"https://codeload.github.com/quasilyte/phpgrep/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quasilyte%2Fphpgrep/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267145661,"owners_count":24042651,"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","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["ast","code","code-search","go","golang","grep","php","php-grep","phpgrep","search","structural-search","structured-search","syntax"],"created_at":"2024-08-04T09:01:07.841Z","updated_at":"2025-10-24T01:10:51.537Z","avatar_url":"https://github.com/quasilyte.png","language":"Go","readme":"# phpgrep\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/quasilyte/phpgrep)](https://goreportcard.com/report/github.com/quasilyte/phpgrep)\n[![GoDoc](https://godoc.org/github.com/quasilyte/phpgrep?status.svg)](https://godoc.org/github.com/quasilyte/phpgrep)\n![Build Status](https://github.com/quasilyte/phpgrep/workflows/Go/badge.svg)\n\nSyntax-aware grep for PHP code.\n\nThis repository is used for the library and command-line tool development.\nA good source for additional utilities and ready-to-run recipes is [phpgrep-contrib](https://github.com/quasilyte/phpgrep-contrib) repository.\n\n## Overview\n\n`phpgrep` is both a library and a command-line tool.\n\nLibrary can be used to perform syntax-aware PHP code matching inside Go programs\nwhile binary utility can be used from your favorite text editor or terminal emulator.\n\nIt's very close to the [structural search and replace](https://www.jetbrains.com/help/phpstorm/structural-search-and-replace.html)\nin PhpStorm, but better suited for standalone usage.\n\nIn many ways, it's inspired by [github.com/mvdan/gogrep/](https://github.com/mvdan/gogrep/).\n\nSee also: [\"phpgrep: syntax aware code search\"](https://speakerdeck.com/quasilyte/phpgrep-syntax-aware-code-search).\n\n## Quick start\n\n\u003e If you're using VS Code, you might be interested in [vscode-phpgrep](https://marketplace.visualstudio.com/items?itemName=quasilyte.phpgrep) extension.\n\nDownload a `phpgrep` binary from the [latest release](https://github.com/quasilyte/phpgrep/releases/), put it somewhere under your `$PATH`.\n\nRun a `-help` command to verify that everything is okay.\n\n```bash\n$ phpgrep -help\nUsage: phpgrep [flags...] targets pattern [filters...]\nWhere:\n  flags are command-line arguments that are listed in -help (see below)\n  targets is a comma-separated list of file or directory names to search in\n  pattern is a string that describes what is being matched\n  filters are optional arguments bound to the pattern\n\nExamples:\n  # Find f calls with a single varible argument.\n  phpgrep file.php 'f(${\"var\"})'\n\n  # Like the previous example, but searches inside entire\n  # directory recursively and variable names are restricted\n  # to $id, $uid and $gid.\n  # Also uses -v flag that makes phpgrep output more info.\n  phpgrep -v ~/code/php 'f(${\"x:var\"})' 'x=id,uid,gid'\n\n  # Run phpgrep on 2 folders (recursively).\n  phpgrep dir1,dir2 '\"some string\"'\n\n  # Print only matches, without locations.\n  phpgrep -format '{{.Match}}' file.php 'pattern'\n\n  # Print only assignments right-hand side.\n  phpgrep -format '{{.rhs}}' file.php '$_ = $rhs'\n\n  # Ignore vendored source code inside project.\n  phpgrep --exclude '/vendor/' project/ 'pattern'\n\nCustom output formatting is possible via the -format flag template.\n  {{.Filename}}  match containing file name\n  {{.Line}}      line number where the match started\n  {{.MatchLine}} a source code line that contains the match\n  {{.Match}}     an entire match string\n  {{.x}}         $x submatch string (can be any submatch name)\n\nThe output colors can be configured with \"--color-\u003cname\u003e\" flags.\nUse --no-color to disable the output coloring.\n\nExit status:\n  0 if something is matched\n  1 if nothing is matched\n  2 if error occurred\n\n# ... rest of output\n```\n\nCreate a test file `hello.php`:\n\n```php\n\u003c?php\nfunction f(...$xs) {}\nf(10);\nf(20);\nf(30); // aha!\nf($x);\nf();\n```\n\nRun `phpgrep` over that file:\n\n```bash\n$ phpgrep hello.php 'f(${\"x:int\"})' 'x!=20'\nhello.php:3: f(10);\nhello.php:5: f(30); // aha!\nfound 2 matches\n```\n\nWe found all `f` calls with a **single** argument `x` that is `int` literal **not equal** to 20.\n\nNext thing to learn is `${\"*\"}` matcher.\n\nSuppose you need to match all `foo` function calls that have `null` argument.\u003cbr\u003e\n`foo` is variadic, so it's unknown where that argument can be located.\n\nThis pattern will match `null` arguments at any position: `foo(${\"*\"}, null, ${\"*\"})`.\n\nRead [pattern language docs](/pattern_language.md) to learn more about how to write search patterns.\n\nRead the [user manual](/docs/user_manual.md) to learn more about `phpgrep` command line arguments and to get some insights on how to use it.\n\n## Recipes\n\nThis section contains ready-to-use `phpgrep` patterns.\n\n`srcdir` is a target source directory (can also be a single filename).\n\n### Useful recipes\n\n```bash\n# Find arrays with at least 1 duplicated key.\n$ phpgrep srcdir '[${\"*\"}, $k =\u003e $_, ${\"*\"}, $k =\u003e $_, ${\"*\"}]'\n\n# Find where `$x ?: $y` can be applied.\n$ phpgrep srcdir '$x ? $x : $y' # Use `$x ?: $y` instead\n\n# Find where `$x ?? $y` can be applied.\n$ phpgrep srcdir 'isset($x) ? $x : $y'\n\n# Find in_array calls that can be replaced with $x == $y.\n$ phpgrep srcdir 'in_array($x, [$y])'\n\n# Find potential operator precedence issues.\n$ phpgrep srcdir '$x \u0026 $mask == $y' # Should be ($x \u0026 $mask) == $y\n$ phpgrep srcdir '$x \u0026 $mask != $y' # Should be ($x \u0026 $mask) != $y\n\n# Find calls where func args are misplaced.\n$ phpgrep srcdir 'stripos(${\"str\"}, $_)'\n$ phpgrep srcdir 'explode($_, ${\"str\"}, ${\"*\"})'\n\n# Find new calls without parentheses.\n$ phpgrep srcdir 'new $t'\n\n# Find all if statements with a body without {}.\n$ phpgrep srcdir 'if ($cond) $x' 'x!~^\\{'\n# Or without regexp.\n$ phpgrep srcdir 'if ($code) ${\"expr\"}'\n\n# Find all error-supress operator usages.\n$ phpgrep srcdir '@$_'\n\n# Find all == (non-strict) comparisons with null.\n$ phpgrep srcdir '$_ == null'\n```\n\n### Miscellaneous recipes\n\n```bash\n# Find all function calls that have at least one var-argument that has _id suffix.\n$ phpgrep srcdir '$f(${\"*\"}, ${\"x:var\"}, ${\"*\"})' 'x~.*_id$'\n\n# Find foo calls where the second argument is integer literal.\n$ phpgrep srcdir 'foo($_, ${\"int\"})'\n```\n\n### Install from sources\n\nYou'll need Go tools to install `phpgrep` from sources.\n\nTo install `phpgrep` binary under your `$(go env GOPATH)/bin`:\n\n```bash\ngo get -v github.com/quasilyte/phpgrep/cmd/phpgrep\n```\n\nIf `$GOPATH/bin` is under your system `$PATH`, `phpgrep` command should be available after that.\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquasilyte%2Fphpgrep","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquasilyte%2Fphpgrep","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquasilyte%2Fphpgrep/lists"}