This package can parse PHP scripts to extract the names of Classes, Functions and Constants used within the script. The parsed names will then be used to generate a list of use Namespace;
statements for the parsed script. It can parse any script, but only namespaced Classes allows the use of the generated statements.
VSCode Screenshot
It's pretty hard to talk about this Topic, because it's just called "Namespace" ...
The use
statement can be used in Classes to tell PHP, which Function to use internally for Functions used in the Class. It also can boost scripts by pointing PHP to the right Namespace to use. For example, if you call json_encode()
within a Class, PHP searches in the calling Class for a Function with the name json_encode()
, before searching it in the global Namespace (if at all). You can speed up the process with a Backslash before the function name, like: \json_encode()
, but it looks awful. An alternate is to define any used Function, Class and Constant at the very top of the Class with the 'use' statement. This Package is made to simplify the process.
The use
statement also allows quick Aliasing of Functions in namespaced Classes.
namespace Any;
use function myOwnJsonEcode as json_encode;
# if you now call "json_encode()" in the class,
# PHP will use "myOwnJsonEcode()" to execute it.
# create directory if not exist
mkdir -p ~/bin/many
# enter directory
cd ~/bin/many
# clone Many\Dev\Used
git clone https://github.com/eypsilon/get-used.git
# make it executable (user+group = rwx)
chmod -v 770 ~/bin/many/get-used/GetUsed.php
Usage from Terminal
~/bin/many/get-used/GetUsed.php /path/to/src/AnyClass.php
Via Web interface using PHPs dev-server
cd ~/bin/many/get-used/www/used
php -S localhost:8000
and open localhost:8000
Feel free to set one you feel comfortable with.
~$ sudo gedit ~/.bash_aliases
# put
alias GetUsed='~/bin/many/get-used/GetUsed.php'
# refresh aliases
~$ source ~/.bash_aliases
GetUsed /path/to/src/AnyClass.php
# Get help -h | info -i | config -c
GetUsed -h
You can use this Package also in VSCode. Set a key combination in ~/.config/Code/User/keybindings.json
{
"key": "ctrl+shift+t",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "GetUsed ${file}\u000D" }
}
and hit the combo on open Files to get use Namespace;
statements on the fly.
If the generated use Namespace;
statements are already defined in the script, the generated ones will get commented out.
GetUsed /path/to/src/Http/Curler.php
// file = /path/to/src/Http/Curler.php
// start = 1661266197.6779
// end = 1661266197.7762
/** defined(0), taken(0), constant(2), class(2), function(2), total(6) */
use DateTime;
use DateTimeZone;
use function array_keys;
use function array_merge;
use const PHP_EOL;
use const JSON_UNESCAPED_SLASHES;
Web Interface for GetUsed
Terminal The screenshot is taken from the web interface, but it looks identical in Terminal.