Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9eba9cf

Browse files
Add file collection to generate classes and interfaces - Close #54
1 parent 02e6fdc commit 9eba9cf

File tree

5 files changed

+212
-2
lines changed

5 files changed

+212
-2
lines changed

‎src/Builder/ClassBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use PhpParser\NodeVisitor;
2424
use PhpParser\Parser;
2525

26-
final class ClassBuilder
26+
final class ClassBuilderimplements File
2727
{
2828
/** @var string|null */
2929
private $namespace;

‎src/Builder/ClassBuilderCollection.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Countable;
1414
use Iterator;
1515

16+
/**
17+
* @deprecated Use FileCollection instead
18+
*/
1619
final class ClassBuilderCollection implements Iterator, Countable
1720
{
1821
/**

‎src/Builder/File.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeAst\Builder;
12+
13+
use PhpParser\NodeTraverser;
14+
use PhpParser\NodeVisitor;
15+
use PhpParser\Parser;
16+
17+
interface File
18+
{
19+
public function getNamespace(): ?string;
20+
21+
public function getName(): ?string;
22+
23+
/**
24+
* @param Parser $parser
25+
* @return NodeVisitor[]
26+
*/
27+
public function generate(Parser $parser): array;
28+
29+
public function injectVisitors(NodeTraverser $nodeTraverser, Parser $parser): void;
30+
31+
public function hasConstant(string $constantName): bool;
32+
33+
/**
34+
* Removing will not work on existing files
35+
*
36+
* @param string ...$constants
37+
* @return $this
38+
*/
39+
public function removeConstant(string ...$constants): self;
40+
41+
/**
42+
* Uses uasort internally
43+
*
44+
* @param callable $sort (ClassConstBuilder $a, ClassConstBuilder $b)
45+
* @return self
46+
*/
47+
public function sortConstants(callable $sort): self;
48+
49+
public function hasMethod(string $methodName): bool;
50+
51+
/**
52+
* Removing will not work on existing files
53+
*
54+
* @param string ...$methodNames
55+
* @return $this
56+
*/
57+
public function removeMethod(string ...$methodNames): self;
58+
59+
/**
60+
* Uses uasort internally
61+
*
62+
* @param callable $sort (ClassMethodBuilder $a, ClassMethodBuilder $b)
63+
* @return self
64+
*/
65+
public function sortMethods(callable $sort): self;
66+
67+
/**
68+
* Uses uasort internally
69+
*
70+
* @param callable $sort (string $a, string $b)
71+
* @return self
72+
*/
73+
public function sortNamespaceImports(callable $sort): self;
74+
75+
/**
76+
* Removing will not work on existing files
77+
*
78+
* @param string ...$namespaceImports
79+
* @return $this
80+
*/
81+
public function removeNamespaceImport(string ...$namespaceImports): self;
82+
83+
public function hasNamespaceImport(string $namespace): bool;
84+
}

‎src/Builder/FileCollection.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-ast for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-ast/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-ast/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeAst\Builder;
12+
13+
use Countable;
14+
use Iterator;
15+
16+
final class FileCollection implements Iterator, Countable
17+
{
18+
/**
19+
* @var array<string, File>
20+
*/
21+
private $items;
22+
23+
public static function fromItems(File ...$files): self
24+
{
25+
return new self(...$files);
26+
}
27+
28+
public static function emptyList(): self
29+
{
30+
return new self();
31+
}
32+
33+
private function __construct(File ...$files)
34+
{
35+
foreach ($files as $file) {
36+
$this->items[$this->identifier($file)] = $file;
37+
}
38+
}
39+
40+
public function add(File $file): self
41+
{
42+
$this->items[$this->identifier($file)] = $file;
43+
44+
return $this;
45+
}
46+
47+
public function remove(File $file): self
48+
{
49+
unset($this->items[$this->identifier($file)]);
50+
51+
return $this;
52+
}
53+
54+
public function contains(File $file): bool
55+
{
56+
return isset($this->items[$this->identifier($file)]);
57+
}
58+
59+
public function filter(callable $filter): self
60+
{
61+
return new self(...\array_values(
62+
\array_filter(
63+
$this->items,
64+
static function (File $file) use ($filter) {
65+
return $filter($file);
66+
}
67+
)
68+
)
69+
);
70+
}
71+
72+
/**
73+
* @return array<string, File>
74+
*/
75+
public function items(): array
76+
{
77+
return $this->items;
78+
}
79+
80+
public function rewind(): void
81+
{
82+
\reset($this->items);
83+
}
84+
85+
public function current(): File
86+
{
87+
return \current($this->items);
88+
}
89+
90+
public function key(): string
91+
{
92+
return \key($this->items);
93+
}
94+
95+
public function next(): void
96+
{
97+
\next($this->items);
98+
}
99+
100+
public function valid(): bool
101+
{
102+
return \key($this->items) !== null;
103+
}
104+
105+
public function count(): int
106+
{
107+
return \count($this->items);
108+
}
109+
110+
private function identifier(File $file): string
111+
{
112+
$namespace = $file->getNamespace() !== null ? ('\\' . $file->getNamespace()) : '';
113+
$name = $file->getName() !== null ? ('\\' . $file->getName()) : '';
114+
115+
$identifier = $namespace . $name;
116+
117+
if ($identifier === '') {
118+
return \spl_object_hash($file);
119+
}
120+
121+
return $identifier;
122+
}
123+
}

‎src/Builder/InterfaceBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use PhpParser\NodeVisitor;
2222
use PhpParser\Parser;
2323

24-
final class InterfaceBuilder
24+
final class InterfaceBuilderimplements File
2525
{
2626
/** @var string|null */
2727
private $namespace;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /