Skip to content

Commit 3ae2d86

Browse files
committed
Typos & Script fixes, removed unused ressources
1 parent bce308b commit 3ae2d86

5 files changed

Lines changed: 278 additions & 272 deletions

File tree

GetUsed.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
require_once "{$base}/src/Used.php";
88
$getCnf = require_once "{$base}/GetUsed.config.php";
99

10-
/** @var mixed parsed arguments */
10+
/**
11+
* @var mixed parsed arguments
12+
*/
1113
parse_str(implode('&', $argv), $argv);
1214

1315
/**
1416
* @var mixed If "file=" is left out in command
1517
*/
16-
if (!isset($argv['file']) AND $pFilename = ($_SERVER['argv'][1] ?? false))
17-
if (is_file($pFilename))
18-
$argv['file'] = $pFilename;
18+
if (!isset($argv['file']) AND $pFilename = ($_SERVER['argv'][1] ?? false) AND is_file($pFilename))
19+
$argv['file'] = $pFilename;
1920

2021
/**
2122
* @var mixed set args
@@ -31,14 +32,18 @@
3132
exit(json_encode([$key => $getCnf[$key]], JSON_PRETTY_PRINT) . PHP_EOL);
3233

3334
/**
34-
* @var string get use Keywords
35+
* @var array get use Statements
3536
*/
36-
$getUsed = (new Used)->get($argv['file'], $argv);
37+
try {
38+
$getUsed = (new Used)->get($argv['file'], $argv);
39+
} catch(Exception $e) {
40+
$getUsed['print'] = $e->getMessage();
41+
}
3742

3843
/**
3944
* @var string response
4045
*/
41-
if ($argv['file'] AND $getUsed) {
46+
if ($getUsed['print'] ?? null) {
4247
$r = [
4348
'file' => trim($argv['file']),
4449
'start' => $_SERVER['REQUEST_TIME_FLOAT'] ?? null,
@@ -50,9 +55,9 @@
5055
} else {
5156
$rPrint = [];
5257
foreach($r as $k => $v)
53-
if (!in_array($k, ['class', 'function', 'constant', 'file_content']))
58+
if (!in_array($k, ['class', 'function', 'constant']))
5459
$rPrint[] = 'print' === $k ? "\n{$v}" : "// {$k} = {$v}";
5560
$r = sprintf('%1$s%2$s%1$s%1$s', PHP_EOL, implode(PHP_EOL, $rPrint));
5661
}
5762
exit($r);
58-
} else exit(sprintf('Error processing the file: %s', $argv['file']));
63+
} exit(sprintf('Error processing the file: %s', $argv['file']));

README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
# Namespace helper - use Namespace\Keywords;
2-
3-
This package can parse PHP scripts to extract the names of PHPs internal Classes, Functions and Constants used within a Class. The parsed Names can then be used to define `use Namespace\Keywords;` in namespaced Classes.
1+
# Namespace helper - create `use Namespace;` Statements
42

3+
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.
54

65
__VSCode Screenshot__
76

87
It's pretty hard to talk about this Topic, because it's just called "Namespace" ...
98

109
![Visual Studio Code Example Response](/www/used/assets/screenshot-vscode.png)
1110

12-
The `use` keyword can be used within Classes to tell PHP, which Classes, Functions and Constants to use internally for Functions used in your Class. It also can boost your 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()` first, 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 IDK, it looks awful. An alternate is to define any used Function, Class and Constant at the very top of your Class with the 'use'-keyword. And that's for what this Package is meant to be.
11+
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.
1312

14-
The `use` keyword also allows you to Alias and change your scripts quick and easy.
13+
The `use` statement also allows quick Aliasing of Functions in namespaced Classes.
1514

1615
```php
16+
namespace Any;
1717
use function myOwnJsonEcode as json_encode;
18-
# if you now call "json_encode()" in your class,
18+
# if you now call "json_encode()" in the class,
1919
# PHP will use "myOwnJsonEcode()" to execute it.
2020
```
2121

@@ -38,7 +38,7 @@ chmod -v 770 ~/bin/many/get-used/GetUsed.php
3838
__Usage from Terminal__
3939

4040
```sh
41-
~/bin/many/get-used/GetUsed.php file="/path/to/src/AnyClass.php"
41+
~/bin/many/get-used/GetUsed.php /path/to/src/AnyClass.php
4242
```
4343

4444
__Via [Web interface](./www/used/) using PHPs dev-server__
@@ -64,8 +64,6 @@ alias GetUsed='~/bin/many/get-used/GetUsed.php'
6464
~$ source ~/.bash_aliases
6565
```
6666
```sh
67-
GetUsed file="/path/to/src/AnyClass.php"
68-
# or
6967
GetUsed /path/to/src/AnyClass.php
7068
# Get help -h | info -i | config -c
7169
GetUsed -h
@@ -84,13 +82,13 @@ You can use this Package also in VSCode. Set a key combination in `~/.config/Cod
8482
}
8583
```
8684

87-
and hit the combo on open Files to get `use Keywords;` on the fly.
85+
and hit the combo on open Files to get `use Namespace;` statements on the fly.
8886

8987
---
9088

9189
#### Example output
9290

93-
If the generated `use Keywords;` are already defined in the target Class, the generated ones will get commented out.
91+
If the generated `use Namespace;` statements are already defined in the script, the generated ones will get commented out.
9492

9593
```sh
9694
GetUsed /path/to/src/Http/Curler.php
@@ -101,7 +99,7 @@ GetUsed /path/to/src/Http/Curler.php
10199
// start = 1661266197.6779
102100
// end = 1661266197.7762
103101

104-
/** lines(-), defined(0), taken(0), constant(2), class(2), function(2), total(6) */
102+
/** defined(0), taken(0), constant(2), class(2), function(2), total(6) */
105103

106104
use DateTime;
107105
use DateTimeZone;

0 commit comments

Comments
 (0)