Skip to content

Commit 4c8ee0b

Browse files
author
dereuromark
committed
Fix up method replacement.
1 parent e3f3675 commit 4c8ee0b

File tree

7 files changed

+75
-28
lines changed

7 files changed

+75
-28
lines changed

src/Annotation/AbstractAnnotation.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,24 @@ public function replaceClassName($type) {
4646
abstract public function build();
4747

4848
/**
49-
* @return string
49+
* @param \IdeHelper\Annotation\AbstractAnnotation $annotation
50+
*
51+
* @return bool
5052
*/
51-
public function __toString() {
52-
return static::TAG . ' ' . $this->build();
53-
}
53+
abstract public function matches(self $annotation);
5454

5555
/**
56-
* @param string $type
56+
* @param \IdeHelper\Annotation\AbstractAnnotation $annotation
57+
*
5758
* @return void
5859
*/
59-
public function setType($type) {
60-
$this->type = $type;
60+
abstract public function replaceWith(self $annotation);
61+
62+
/**
63+
* @return string
64+
*/
65+
public function __toString() {
66+
return static::TAG . ' ' . $this->build();
6167
}
6268

6369
/**
@@ -66,7 +72,6 @@ public function setType($type) {
6672
public function getType() {
6773
return $this->type;
6874
}
69-
7075
/**
7176
* @return int|null
7277
*/
@@ -78,6 +83,4 @@ public function getIndex() {
7883
return $this->index;
7984
}
8085

81-
abstract public function matches(self $annotation);
82-
8386
}

src/Annotation/MethodAnnotation.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,21 @@ public function matches(AbstractAnnotation $annotation) {
4444
if ($annotation::TAG !== static::TAG) {
4545
return false;
4646
}
47-
if ($annotation->getMethod() !== $this->method) {
47+
$methodName = substr($annotation->getMethod(), 0, strpos($annotation->getMethod(), '('));
48+
if ($methodName !== substr($this->method, 0, strpos($this->method, '('))) {
4849
return false;
4950
}
5051

5152
return true;
5253
}
5354

55+
/**
56+
* @param \IdeHelper\Annotation\AbstractAnnotation|\IdeHelper\Annotation\MethodAnnotation $annotation
57+
* @return void
58+
*/
59+
public function replaceWith(AbstractAnnotation $annotation) {
60+
$this->type = $annotation->getType();
61+
$this->method = $annotation->getMethod();
62+
}
63+
5464
}

src/Annotation/PropertyAnnotation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,13 @@ public function matches(AbstractAnnotation $annotation) {
5151
return true;
5252
}
5353

54+
/**
55+
* @param \IdeHelper\Annotation\AbstractAnnotation|\IdeHelper\Annotation\PropertyAnnotation $annotation
56+
* @return void
57+
*/
58+
public function replaceWith(AbstractAnnotation $annotation) {
59+
$this->type = $annotation->getType();
60+
$this->property = $annotation->getProperty();
61+
}
62+
5463
}

src/Annotator/AbstractAnnotator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ protected function _appendToExistingDocBlock(PHP_CodeSniffer_File $file, $closeT
246246
protected function _needsReplacing(AbstractAnnotation $annotation, array $existingAnnotations) {
247247
foreach ($existingAnnotations as $existingAnnotation) {
248248
if ($existingAnnotation->matches($annotation)) {
249-
$existingAnnotation->setType($annotation->getType());
249+
$existingAnnotation->replaceWith($annotation);
250250
return $existingAnnotation;
251251
}
252252
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace IdeHelper\Test\TestCase\Annotation;
4+
5+
use IdeHelper\Annotation\AnnotationFactory;
6+
use IdeHelper\Annotation\MethodAnnotation;
7+
use IdeHelper\Annotation\PropertyAnnotation;
8+
use Tools\TestSuite\TestCase;
9+
10+
/**
11+
*/
12+
class AnnotationFactoryTest extends TestCase {
13+
14+
/**
15+
* @return void
16+
*/
17+
public function setUp() {
18+
parent::setUp();
19+
}
20+
21+
/**
22+
* @return void
23+
*/
24+
public function testCreate() {
25+
$annotation = AnnotationFactory::create('@method', '\\Foo\\Model\\Entity\\Bar', 'doSth()', 1);
26+
$this->assertInstanceOf(MethodAnnotation::class, $annotation);
27+
28+
$annotation = AnnotationFactory::create('@property', '\\Foo\\Model\\Entity\\Bar', '$baz', 1);
29+
$this->assertInstanceOf(PropertyAnnotation::class, $annotation);
30+
}
31+
32+
}

tests/TestCase/Annotation/MethodAnnotationTest.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,14 @@ public function testBuild() {
3030
/**
3131
* @return void
3232
*/
33-
public function testSetType() {
34-
$annotation = new MethodAnnotation('\\Foo\\Model\\Entity\\Bar', 'doSth()');
35-
$annotation->setType('\\Something\\Model\\Entity\\Else');
36-
37-
$result = (string)$annotation;
38-
$this->assertSame('@method \\Something\\Model\\Entity\\Else doSth()', $result);
39-
}
33+
public function testReplaceWith() {
34+
$replacementAnnotation = new MethodAnnotation('\\Something\\Model\\Entity\\Else', 'doSth(array $options = [])');
4035

41-
/**
42-
* @return void
43-
*/
44-
public function testSetTypeComplex() {
45-
$annotation = new MethodAnnotation('\\Foo\\Model\\Entity\\Bar[]|bool', 'doSth()');
46-
$annotation->setType('\\Something\\Model\\Entity\\Else[]|bool');
36+
$annotation = new MethodAnnotation('\\Foo\\Model\\Entity\\Bar', 'doSth()');
37+
$annotation->replaceWith($replacementAnnotation);
4738

4839
$result = (string)$annotation;
49-
$this->assertSame('@method \\Something\\Model\\Entity\\Else[]|bool doSth()', $result);
40+
$this->assertSame('@method \\Something\\Model\\Entity\\Else doSth(array $options = [])', $result);
5041
}
5142

5243
/**

tests/TestCase/Annotation/PropertyAnnotationTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ public function testBuild() {
3030
/**
3131
* @return void
3232
*/
33-
public function testSetType() {
33+
public function testReplaceWith() {
34+
$replacementAnnotation = new PropertyAnnotation('\\Something\\Model\\Table\\Else', '$baz');
35+
3436
$annotation = new PropertyAnnotation('\\Foo\\Model\\Table\\Bar', '$baz');
35-
$annotation->setType('\\Something\\Model\\Table\\Else');
37+
$annotation->replaceWith($replacementAnnotation);
3638

3739
$result = (string)$annotation;
3840
$this->assertSame('@property \\Something\\Model\\Table\\Else $baz', $result);

0 commit comments

Comments
 (0)