Repository navigation
Expand file tree
/
Copy pathcode_lens.php
More file actions
76 lines (59 loc) · 3 KB
/
Copy pathcode_lens.php
File metadata and controls
76 lines (59 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* PHP Showcase — Code Lens
*
* The annotations rendered above a declaration.
*
* One of the demo files listed in README.md. Supporting fixtures live in
* scaffolding/scaffolding.php (namespace Demo\Scaffolding), and the runtime
* assertions that verify the type claims in the comments below live in
* scaffolding/assertions.php.
*/
namespace Demo;
use Demo\Scaffolding;
// ── Code Lens: prototype method annotations ─────────────────────────────────
// Open this class and look at the gutter above each method. PHPantom shows
// clickable annotations ("↑ ParentClass::method" or "◆ Interface::method")
// that navigate to the parent/interface declaration.
class CodeLensDemo extends Scaffolding\ScaffoldingAbstractShape implements Scaffolding\ScaffoldingDrawable
{
// ↑ Scaffolding\ScaffoldingAbstractShape::area — click to jump to abstract declaration
public function area(): float { return 3.14; }
// ↑ Scaffolding\ScaffoldingAbstractShape::perimeter
protected function perimeter(): float { return 6.28; }
// ◆ Scaffolding\ScaffoldingDrawable::draw — interface implementations use ◆
public function draw(string $color, float $opacity = 1.0): void {}
}
// ── Code Lens: reference counts on declarations ─────────────────────────────
// Above a declaration PHPantom shows how many places use it, for a class,
// method, property, constant, or a function declared outside any class, so a
// plain helpers file gets them as well. Click the count to list the usages.
function codeLensFormatLabel(string $text): string
{
return ucfirst($text);
}
// codeLensFormatLabel above shows "2 references": the two calls below.
$codeLensFirstLabel = codeLensFormatLabel('first');
$codeLensSecondLabel = codeLensFormatLabel('second');
// A function nothing calls shows "0 references", which is the quickest way
// to spot dead code in a procedural file.
function codeLensUnusedHelper(): void {}
// ── Code Lens: implementation counts ────────────────────────────────────────
// Above an interface or abstract class, and above each method it declares,
// PHPantom shows how many classes implement it. Click the count to list them.
// "2 implementations": CodeLensJsonExporter and CodeLensCsvExporter.
interface CodeLensExporter
{
// "2 implementations": the method in CodeLensJsonExporter, and the one
// CodeLensCsvExporter inherits unchanged from CodeLensTextExporter.
public function export(array $rows): string;
}
final class CodeLensJsonExporter implements CodeLensExporter
{
public function export(array $rows): string { return '[' . implode(',', $rows) . ']'; }
}
class CodeLensTextExporter
{
public function export(array $rows): string { return implode("\n", $rows); }
}
final class CodeLensCsvExporter extends CodeLensTextExporter implements CodeLensExporter {}