Elevate your PHP development in Neovim with PhpTools - bringing you one step closer to PHPStorm-like functionality ;).
phptools-faster.mp4
- Generate undefined methods
- Create classes, traits, interfaces, and enums with proper namespacing, including
use
statements - 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
- Run PHPUnit/Pest tests
- Drupal autoloader - automatically manages PSR-4 autoloading for Drupal modules
- Laravel IDE Helper - automatically generates ide helpers
- Laravel, Symfony, Drupal compatible
Command: :PhpTools Method
Generates undefined methods under cursor. Works with:
- Object methods:
$router->resolve();
,$this->container->get(Router::class);
,$this->get()
- Static methods:
Router::resolve();
- Instantiated methods:
(new Router)->resolve();
- Static :
Router::findroot()
- Enum :
Color::RED
If the class doesn't exist, it will also generate the class.
Command: :PhpTools Class
Creates undefined classes, traits, interfaces, or enums under cursor. Supports:
- Class instantiation:
new Router();
- Class declaration:
class Router extends|implements Route
- Trait usage:
use TraitName;
- Enum declaration:
Color::RED
- Static declaration:
Router::findroot()
- Simple Parameter:
function foo(Router $router) {}
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: :PhpTools Namespace
Generates the appropriate namespace for the current file based on its location in the project structure.
Command: :PhpTools GetSet
When the cursor is on a property declaration (e.g., public array $routes = [];
), it generates getter, setter, or both for that property.
Command: :PhpTools Create
Allows you to create a new PHP entity (Class, Interface, Enum, or Trait) in the current file, complete with the correct namespace.
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
- 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: :PhpTools 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!";
}
PhpTools.nvim provides comprehensive test running capabilities for PHP projects using PHPUnit or Pest.
- Automatically detects and uses PHPUnit or Pest test runner
- Supports multiple test patterns:
- PHPUnit method annotations (
@test
) - Test method prefixes (
test_*
) - Pest test definitions (
test()
,it()
)
- PHPUnit method annotations (
- Interactive test output in a floating window
- Press
q
orEsc
to close - Use
gf
to jump to failed test file
- Press
- Smart test detection:
- Finds nearest test based on cursor position
- Supports both class-based and function-based tests
- Run all tests in project
- Run single test file
- Filter and run specific tests
- Parallel test execution support
- Re-run last test
Screen.Recording.2024-11-02.at.7.55.50.PM.mov
PhpTools.nvim includes a powerful Drupal autoloader that automatically manages PSR-4 autoloading for Drupal modules. This feature helps keep your autoload configurations up-to-date as you develop Drupal modules.
- Automatically scans contributed modules directory
- Updates PSR-4 autoload configurations
- Watches for changes in composer.json and autoload files
- Maintains proper namespacing for Drupal modules
PhpTools.nvim includes built-in support for Laravel IDE Helper.
- Generate helpers for facades, models, and meta files
- Interactive model selection for targeted generation
- One-command installation and setup
- Progress notifications for long-running operations
- Auto-generates model helpers when saving files in
app/Models/
- Shows progress notifications during helper generation
- Validates Laravel project structure before operations
Using lazy.nvim:
{
'ccaglak/phptools.nvim',
keys = {
{ "<leader>lm", "<cmd>PhpTools Method<cr>"},
{ "<leader>lc", "<cmd>PhpTools Class<cr>"},
{ "<leader>ls", "<cmd>PhpTools Scripts<cr>"},
{ "<leader>ln", "<cmd>PhpTools Namespace<cr>"},
{ "<leader>lg", "<cmd>PhpTools GetSet<cr>"},
{ "<leader>lf", "<cmd>PhpTools Create<cr>"},
{ "<leader>ld", "<cmd>PhpTools DrupalAutoLoader<cr>"},
{ mode="v", "<leader>lr", "<cmd>PhpTools Refactor<cr>"},
},
dependencies = {
-- "ccaglak/namespace.nvim", -- optional - php namespace resolver
-- "ccaglak/larago.nvim", -- optional -- laravel goto blade/components
-- "ccaglak/snippets.nvim", -- optional -- native snippet expander
},
config = function()
require('phptools').setup({
ui = {
enable = true, -- default:true; false only if you have a UI enhancement plugin
fzf = false -- default:false; tests requires fzf used only in tests module otherwise there might long list of tests
},
drupal_autoloader = { -- delete if you dont use it
scan_paths = { "/web/modules/contrib/" }, -- Paths to scan for modules
root_markers = { ".git" }, -- Project root markers
autoload_file = "/vendor/composer/autoload_psr4.php" -- Autoload file path
},
custom_toggles = { -- delete if you dont use it
-- { "foo", "bar", "baz" }, -- Add more custom toggle groups here
}
})
local map = vim.keymap.set
local ide_helper = require('phptools.ide_helper') -- delete if you dont use it
-- Laravel IDE Helper keymaps
map('n', '<leader>lha', ide_helper.generate_all, { desc = 'Generate all IDE helpers' })
map('n', '<leader>lhm', ide_helper.generate_models, { desc = 'Generate model helpers' })
map('n', '<leader>lhf', ide_helper.generate_facades, { desc = 'Generate facade helpers' })
map('n', '<leader>lht', ide_helper.generate_meta, { desc = 'Generate meta helper' })
map('n', '<leader>lhi', ide_helper.install, { desc = 'Install IDE Helper package' })
local tests = require("phptools.tests") -- delete if you have a test plugin
map("n", "<Leader>ta", tests.test.all, { desc = "Run all tests" })
map("n", "<Leader>tf", tests.test.file, { desc = "Run current file tests" })
map("n", "<Leader>tl", tests.test.line, { desc = "Run test at cursor" })
map("n", "<Leader>ts", tests.test.filter, { desc = "Search and run test" })
map("n", "<Leader>tp", tests.test.parallel, { desc = "Run tests in parallel" })
map("n", "<Leader>tr", tests.test.rerun, { desc = "Rerun last test" })
map("n", "<Leader>ti", tests.test.selected, { desc = "Run selected test file" })
end
}
- ripgrep
- nvim-treesitter (
:TSInstall php json
)
- running out of ideas for now
- your welcome to contribute or suggest features
- PHP Namespace Resolver namespace.nvim.
- Snippets.nvim snippets.nvim.
- Laravel Goto Blade/Components larago.nvim.
- by PHPStorm