Elevate your PHP development in Neovim with PhpTools - bringing you one step closer to PHPStorm-like functionality.
phptools-faster.mp4
- Generate undefined methods and classes
- Create classes, traits, interfaces, and enums with proper namespacing
- Run Composer scripts
- Generate namespaces for files
- Create getters and setters
- Create PHP entities (Class, Interface, Enum, Trait) with namespaces
- Toggles common words / or fallbacks
- Refactor with common structures and control flow statements
Command: :Php Method
Generates undefined methods. Works with:
- Object methods:
$router->resolve();
- Static methods:
Router::resolve();
- Instantiated methods:
(new Router)->resolve();
- Enum declaration:
Router::findroot()
If the class doesn't exist, it will also generate the class.
Command: :Php Class
Creates undefined classes, traits, interfaces, or enums. Supports:
- Class instantiation:
new Router();
- Class declaration:
class Router extends|implements Route
- Trait usage:
use TraitName;
- Enum declaration:
Router::findroot()
Generates the entity with proper namespace and creates a use statement for the current file.
Command: :Php Scripts
Runs Composer scripts defined in your composer.json
file.
Command: :Php Namespace
Generates the appropriate namespace for the current file based on its location in the project structure.
Command: :Php GetSet
When the cursor is on a property declaration (e.g., public array $routes = [];
), it generates getter, setter, or both for that property.
Command: :Php Create
Allows you to create a new PHP entity (Class, Interface, Enum, or Trait) in the current file, complete with the correct namespace.
To use these commands effectively, map them to convenient keybindings as shown in the installation section. This will allow quick access to PhpTools functionality while coding.
PhpTools.nvim includes a powerful toggle feature that enhances your PHP development workflow. This feature allows you to quickly switch between related keywords, operators, and values with simple key presses.
-
Word Toggling: Easily cycle through related PHP keywords and types.
- Examples:
public
<->protected
<->private
self
<->static
true
<->false
require
<->include
abstract
<->final
class
<->interface
<->trait
<->enum
string
<->int
<->float
<->bool
<->array
array_map
<->array_filter
<->array_reduce
- Examples:
-
Operator Toggling: Quickly switch between related operators.
- Examples:
==
<->===
!=
<->!==
>
<->>=
<
<-><=
&&
<->||
++
<->--
->
<->=>
- Examples:
-
Custom Toggles: Add your own custom toggle groups to suit your specific needs.
-
In normal mode, place your cursor on a word or operator and press:
<C-a>
to cycle forward through toggles<C-x>
to cycle backward through toggles
-
If the word or operator under the cursor isn't part of a toggle group, it will increment or decrement numbers as usual.
Command: :Php Refactor
Quickly surround your PHP code with common structures and control flow statements.
-
Select the text you want to surround in visual/visualline mode
-
Execute the
:Php Refactor
command -
Choose from the available options:
if
: Wraps the selection in an if statementfor
: Creates a for loop around the selectionforeach
: Surrounds the selection with a foreach loopwhile
: Wraps the selection in a while loopdo-while
: Puts the selection inside a do-while looptry-catch
: Encloses the selection in a try-catch blockfunction
: Creates a function around the selectionmethod
: Wraps the selection in a method
// Before (with 'echo "Hello, World!";' selected)
echo "Hello, World!";
// After choosing 'if' from Php Refactor
if (condition) {
echo "Hello, World!";
}
## Installation
Using [lazy.nvim](https://github.com/folke/lazy.nvim):
```lua
{
'ccaglak/phptools.nvim',
keys = {
{ "<leader>lm", "<cmd>Php Method<cr>"},
{ "<leader>lc", "<cmd>Php Class<cr>"},
{ "<leader>ls", "<cmd>Php Scripts<cr>"},
{ "<leader>ln", "<cmd>Php Namespace<cr>"},
{ "<leader>lg", "<cmd>Php GetSet<cr>"},
{ "<leader>lf", "<cmd>Php Create<cr>"},
},
dependencies = {
"nvim-lua/plenary.nvim"
},
config = function()
require('phptools').setup({
custom_toggles = {
-- { "foo", "bar", "baz" },
-- Add more custom toggle groups here
}
ui = false, -- Set to true if not using a UI enhancement plugin
})
vim.keymap.set('v', '<leader>lr', function()
require("phptools.refactor").refactor()
end, { desc = 'PhpRefactor' })
end
}
- pleanery.nvim
- nvim-treesitter (
:TSInstall php json
) - recommended stevearc/dressing.nvim but optional (read config)
- custom templates
- append to codeactions
- custom template per directory base :? in Controller directory, controller template is generated
- run tests (run all, filter, file, line)
- Let me know if you have any edge cases
- Laravel Goto Blade/Components larago.nvim.
- PHP Namespace Resolver namespace.nvim.
- by PHPStorm