All Projects β†’ quasilyte β†’ Phpgrep

quasilyte / Phpgrep

Licence: mit
Syntax-aware grep for PHP code.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Phpgrep

Gogrep
Search for Go code using syntax trees
Stars: ✭ 450 (+143.24%)
Mutual labels:  search, code, syntax
Ugrep
πŸ”NEW ugrep v3.1: ultra fast grep with interactive query UI and fuzzy search: search file systems, source code, text, binary files, archives (cpio/tar/pax/zip), compressed files (gz/Z/bz2/lzma/xz/lz4), documents and more. A faster, user-friendly and compatible grep replacement.
Stars: ✭ 626 (+238.38%)
Mutual labels:  search, grep
Opengrok
OpenGrok is a fast and usable source code search and cross reference engine, written in Java
Stars: ✭ 3,452 (+1765.95%)
Mutual labels:  search, code
Ripgrep
ripgrep recursively searches directories for a regex pattern while respecting your gitignore
Stars: ✭ 28,564 (+15340%)
Mutual labels:  search, grep
asty
Abstract Syntax Tree (AST) Data Structure
Stars: ✭ 28 (-84.86%)
Mutual labels:  syntax, ast
lowcode
React Lowcode - prototype, develop and maintain internal apps easier
Stars: ✭ 32 (-82.7%)
Mutual labels:  code, ast
Code Surfer
Rad code slides <πŸ„/>
Stars: ✭ 5,477 (+2860.54%)
Mutual labels:  code, syntax
Pervane
Plain text file based note taking and knowledge base building tool, markdown editor, simple browser IDE.
Stars: ✭ 159 (-14.05%)
Mutual labels:  search, code
Astq
Abstract Syntax Tree (AST) Query Engine
Stars: ✭ 89 (-51.89%)
Mutual labels:  ast, syntax
Vscode Tsquery
TSQuery extension for Visual Studio Code
Stars: ✭ 13 (-92.97%)
Mutual labels:  ast, search
yode
Yode - Focused Code Editing
Stars: ✭ 28 (-84.86%)
Mutual labels:  code, ast
Query Translator
Query Translator is a search query translator with AST representation
Stars: ✭ 165 (-10.81%)
Mutual labels:  ast, search
gogrep
Syntax-aware Go code search, based on the mvdan/gogrep
Stars: ✭ 25 (-86.49%)
Mutual labels:  syntax, ast
Rg.el
Emacs search tool based on ripgrep
Stars: ✭ 277 (+49.73%)
Mutual labels:  search, grep
micro-code-analyser
A tiny Node.js microservice to detect the language of a code snippet
Stars: ✭ 21 (-88.65%)
Mutual labels:  syntax, code
coAST
Universal and language-independent abstract syntax tree
Stars: ✭ 30 (-83.78%)
Mutual labels:  syntax, ast
ast-grep
πŸ” Like grep, but more powerful than you can possibly imagine
Stars: ✭ 14 (-92.43%)
Mutual labels:  ast, grep
KodeEditor
A simple code editor with syntax highlighting and pinch to zoom
Stars: ✭ 60 (-67.57%)
Mutual labels:  syntax, code
Modiscript
Acche din aa gaye
Stars: ✭ 888 (+380%)
Mutual labels:  ast, syntax
React Ast
render abstract syntax trees with react
Stars: ✭ 160 (-13.51%)
Mutual labels:  ast, syntax

phpgrep

Go Report Card GoDoc Build Status

Syntax-aware grep for PHP code.

This repository is used for the library and command-line tool development. A good source for additional utilities and ready-to-run recipes is phpgrep-contrib repository.

Overview

phpgrep is both a library and a command-line tool.

Library can be used to perform syntax-aware PHP code matching inside Go programs while binary utility can be used from your favorite text editor or terminal emulator.

It's very close to structural search and replace in PhpStorm, but better suited for standalone usage.

In many ways, it's inspired by github.com/mvdan/gogrep/.

See also: "phpgrep: syntax aware code search".

Quick start

If you're using VS Code, you might be interested in vscode-phpgrep extension.

Download a phpgrep binary from the latest release, put it somewhere under your $PATH.

Run a -help command to verify that everything is okay.

$ phpgrep -help
Usage: phpgrep [flags...] targets pattern [filters...]
Where:
  flags are command-line flags that are listed in -help (see below)
  targets is a comma-separated list of file or directory names to search in
  pattern is a string that describes what is being matched
  filters are optional arguments bound to the pattern

Examples:
  # Find f calls with a single varible argument.
  phpgrep file.php 'f(${"var"})'

  # Like the previous example, but searches inside entire
  # directory recursively and variable names are restricted
  # to $id, $uid and $gid.
  # Also uses -v flag that makes phpgrep output more info.
  phpgrep -v ~/code/php 'f(${"x:var"})' 'x=id,uid,gid'

  # Run phpgrep on 2 folders (recursively).
  phpgrep dir1,dir2 '"some string"'

  # Print only matches, without locations.
  phpgrep -format '{{.Match}}' file.php 'pattern'

  # Print only assignments right-hand side.
  phpgrep -format '{{.rhs}}' file.php '$_ = $rhs'

  # Ignore vendored source code inside project.
  phpgrep --exclude '/vendor/' project/ 'pattern'

Custom output formatting is possible via the -format flag template.
  {{.Filename}} match containing file name
  {{.Line}}     line number where the match started
  {{.Match}}    an entire match string
  {{.x}}        $x submatch string (can be any submatch name)

Exit status:
  0 if something is matched
  1 if nothing is matched
  2 if error occurred

# ... rest of output

Create a test file hello.php:

<?php
function f(...$xs) {}
f(10);
f(20);
f(30);
f($x);
f();

Run phpgrep over that file:

$ phpgrep hello.php 'f(${"x:int"})' 'x!=20'
hello.php:3: f(10)
hello.php:5: f(30)

We found all f calls with a single argument x that is int literal not equal to 20.

Next thing to learn is ${"*"} matcher.

Suppose you need to match all foo function calls that have null argument.
foo is variadic, so it's unknown where that argument can be located.

This pattern will match null arguments at any position: foo(${"*"}, null, ${"*"}).

Read pattern language docs to learn more about how to write search patterns.

Recipes

This section contains ready-to-use phpgrep patterns.

srcdir is a target source directory (can also be a single filename).

Useful recipes

# Find arrays with at least 1 duplicated key.
$ phpgrep srcdir '[${"*"}, $k => $_, ${"*"}, $k => $_, ${"*"}]'

# Find where `$x ?: $y` can be applied.
$ phpgrep srcdir '$x ? $x : $y' # Use `$x ?: $y` instead

# Find where `$x ?? $y` can be applied.
$ phpgrep srcdir 'isset($x) ? $x : $y'

# Find in_array calls that can be replaced with $x == $y.
$ phpgrep srcdir 'in_array($x, [$y])'

# Find potential operator precedence issues.
$ phpgrep srcdir '$x & $mask == $y' # Should be ($x & $mask) == $y
$ phpgrep srcdir '$x & $mask != $y' # Should be ($x & $mask) != $y

# Find calls where func args are misplaced.
$ phpgrep srcdir 'stripos(${"str"}, $_)'
$ phpgrep srcdir 'explode($_, ${"str"}, ${"*"})'

# Find new calls without parentheses.
$ phpgrep srcdir 'new $t'

# Find all if statements with a body without {}.
$ phpgrep srcdir 'if ($cond) $x' 'x!~^\{'
# Or without regexp.
$ phpgrep srcdir 'if ($code) ${"expr"}'

# Find all error-supress operator usages.
$ phpgrep srcdir '@$_'

# Find all == (non-strict) comparisons with null.
$ phpgrep srcdir '$_ == null'

Miscellaneous recipes

# Find all function calls that have at least one var-argument that has _id suffix.
$ phpgrep srcdir '$f(${"*"}, ${"x:var"}, ${"*"})' 'x~.*_id$'

# Find foo calls where the second argument is integer literal.
$ phpgrep srcdir 'foo($_, ${"int"})'

Install from sources

You'll need Go tools to install phpgrep from sources.

To install phpgrep binary under your $(go env GOPATH)/bin:

go get -v github.com/quasilyte/phpgrep/cmd/phpgrep

If $GOPATH/bin is under your system $PATH, phpgrep command should be available after that.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].