PHP Files
Extension that adds naive IDE-like file creation.
!IMPORTANT! For now tested only under *nix.
A Story Behind
I like using Visual Studio Code.
But I was not satisfied with the process of creating PHP files. It always involves at least two steps: creation of the file and creation of the content.
Even if you use snippets or templates you have to provide name twice. Even further, if you follow PSR-4 (and nowadays you should try to always follow it) you have to add namespace yourself.
Features
Automatic Namespace Resolution
For now this feature is available to you if you place .php-files.json
to the root of your project:
{
"namespaces": {
"Foo": "src"
}
}
I'm going to provide the ability to retrieve namespaces from composer.json
.
Files creation
For now you have the following options for creating files from Explorer context menu:
New PHP Class
- Select
New PHP Class
from the context menu inside Explorer.
- Enter full PHP Class name (e.g. 'Foo\Bar\Baz') and press enter to proceed or enter escape to discard.
- Enter parent PHP Class name. You can leave it empty and press enter to proceed to the implements,
or short-exit with escape to create class without extends or implements.
- Enter PHP Interfaces names (can be separated by comma).
New PHP Interface
- Select
New PHP Interface
from the context menu inside Explorer.
- Enter full PHP Interface name (e.g. 'Foo\Bar\Baz') and press enter to proceed or enter escape to discard.
- Enter PHP Interfaces names (can be separated by comma).
New PHP Trait
- Select
New PHP Trait
from the context menu inside Explorer.
- Enter full PHP Trait name (e.g. 'Foo\Bar\Baz') and press enter to proceed or enter escape to discard.
Extension Settings
This extension contributes the following settings:
// Template for PHP Class
"php-files.templates.PHPClass": [
"<?php",
"class PHPClass",
"{",
"}"
],
// Template for PHP Interface
"php-files.templates.PHPInterface": [
"<?php",
"interface PHPInterface",
"{",
"}"
],
// Template for PHP Trait
"php-files.templates.PHPTrait": [
"<?php",
"trait PHPTrait",
"{",
"}"
],
Known Issues
When you create new files a bunch of new lines created in the end of it.
I will investigate this. As a temporary solution you can set file of the vscode itself to remove them on save settings:
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
TODO
- Allow using of commands from the command palette.
- Implement tests.
- Implement
PHP File Rename
- Implement
PHP File Duplicate
Acknowledgments
All the hardcore PHP AST staff is done by php_writer, which itself uses following great libraries:
I cannot thank the creators and contributers of those packages enough!