Skip to content

Commit 8540209

Browse files
committed
Add has* methods to ClassBuilder - Close #48
1 parent 9252876 commit 8540209

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

src/Builder/ClassBuilder.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ public function setExtends(string $extends): self
122122
return $this;
123123
}
124124

125-
public function setImplements(string ...$interfaces): self
125+
public function setImplements(string ...$implements): self
126126
{
127-
$this->implements = $interfaces;
127+
$this->implements = [];
128+
129+
foreach ($implements as $implement) {
130+
$this->implements[$implement] = $implement;
131+
}
128132

129133
return $this;
130134
}
@@ -144,6 +148,11 @@ public function addImplement(string ...$implements): self
144148
return $this;
145149
}
146150

151+
public function hasImplement(string $implement): bool
152+
{
153+
return isset($this->implements[$implement]);
154+
}
155+
147156
public function setNamespace(string $namespace): self
148157
{
149158
$this->namespace = $namespace;
@@ -198,6 +207,11 @@ public function removeNamespaceImport(string ...$namespaceImports): self
198207
return $this;
199208
}
200209

210+
public function hasNamespaceImport(string $namespace): bool
211+
{
212+
return isset($this->namespaceImports[$namespace]);
213+
}
214+
201215
/**
202216
* @deprecated Use setNamespaceImports()
203217
* @param string ...$namespaces
@@ -255,6 +269,11 @@ public function removeTrait(string ...$traits): self
255269
return $this;
256270
}
257271

272+
public function hasTrait(string $trait): bool
273+
{
274+
return isset($this->traits[$trait]);
275+
}
276+
258277
/**
259278
* @deprecated Use setTraits()
260279
* @param string ...$traits
@@ -273,7 +292,11 @@ public function setUseTrait(string ...$traits): self
273292
*/
274293
public function setConstants(ClassConstBuilder ...$constants): self
275294
{
276-
$this->constants = $constants;
295+
$this->constants = [];
296+
297+
foreach ($constants as $constant) {
298+
$this->constants[$constant->getName()] = $constant;
299+
}
277300

278301
return $this;
279302
}
@@ -308,6 +331,11 @@ public function removeConstant(string ...$constants): self
308331
return $this;
309332
}
310333

334+
public function hasConstant(string $constantName): bool
335+
{
336+
return isset($this->constants[$constantName]);
337+
}
338+
311339
/**
312340
* Replacing will not work on existing files
313341
*
@@ -355,6 +383,11 @@ public function removeProperty(string ...$propertyNames): self
355383
return $this;
356384
}
357385

386+
public function hasProperty(string $propertyName): bool
387+
{
388+
return isset($this->properties[$propertyName]);
389+
}
390+
358391
public function setMethods(ClassMethodBuilder ...$methods): self
359392
{
360393
$this->methods = [];
@@ -396,6 +429,11 @@ public function removeMethod(string ...$methodNames): self
396429
return $this;
397430
}
398431

432+
public function hasMethod(string $methodName): bool
433+
{
434+
return isset($this->methods[$methodName]);
435+
}
436+
399437
public function getNamespace(): ?string
400438
{
401439
return $this->namespace;

tests/Builder/ClassBuilderTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function it_generates_class_for_empty_file(): void
5252
->setTraits('\\My\\TestTrait')
5353
->setConstants(ClassConstBuilder::fromScratch('PRIV', 'private')->setPrivate());
5454

55+
$this->assertTrue($classFactory->hasConstant('PRIV'));
56+
$this->assertTrue($classFactory->hasImplement('\\Iterator'));
57+
$this->assertTrue($classFactory->hasImplement('Bar'));
58+
$this->assertTrue($classFactory->hasTrait('\\My\\TestTrait'));
59+
$this->assertTrue($classFactory->hasNamespaceImport('Foo\\Bar'));
60+
5561
$nodeTraverser = new NodeTraverser();
5662
$classFactory->injectVisitors($nodeTraverser, $this->parser);
5763

tests/Builder/ClassMethodBuilderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public function it_generates_method_for_empty_class(): void
5252
ClassMethodBuilder::fromScratch('doSomething')->setReturnType('void')->setFinal(true)
5353
);
5454

55+
$this->assertTrue($classFactory->hasMethod('setActive'));
56+
$this->assertTrue($classFactory->hasMethod('doSomething'));
57+
5558
$methods = $classFactory->getMethods();
5659

5760
$this->assertCount(2, $methods);

tests/Builder/ClassPropertyBuilderTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public function it_generates_property_for_empty_class(): void
4646
$classFactory = ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service');
4747
$classFactory->setProperties(ClassPropertyBuilder::fromScratch('aggregateId', 'string'));
4848

49+
$this->assertTrue($classFactory->hasProperty('aggregateId'));
50+
4951
$nodeTraverser = new NodeTraverser();
5052
$classFactory->injectVisitors($nodeTraverser, $this->parser);
5153

0 commit comments

Comments
 (0)