Skip to content

Commit 96a3c97

Browse files
kategraysaimaz
authored andcommitted
Don't throw an exception when a Trait is found in the same directory (#794)
* Don't throw an exception when traits are used (#793) * Fix phpcs warning
1 parent f7277d2 commit 96a3c97

File tree

5 files changed

+103
-2
lines changed

5 files changed

+103
-2
lines changed

Mapping/DocumentParser.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ public function __construct(Reader $reader, DocumentFinder $finder)
8686
*
8787
* @param \ReflectionClass $class
8888
*
89-
* @return array
89+
* @return array|null
9090
* @throws MissingDocumentAnnotationException
9191
*/
9292
public function parse(\ReflectionClass $class)
9393
{
9494
$className = $class->getName();
9595

96+
if ($class->isTrait()) {
97+
return false;
98+
}
99+
96100
if (!isset($this->documents[$className])) {
97101
/** @var Document $document */
98102
$document = $this->reader->getClassAnnotation($class, self::DOCUMENT_ANNOTATION);
@@ -439,7 +443,7 @@ private function getDocumentPropertiesReflection(\ReflectionClass $reflectionCla
439443
$properties = [];
440444

441445
foreach ($reflectionClass->getProperties() as $property) {
442-
if (!array_key_exists($property->getName(), $properties)) {
446+
if (!in_array($property->getName(), $properties)) {
443447
$properties[$property->getName()] = $property;
444448
}
445449
}

Mapping/MetadataCollector.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ public function getBundleMapping($name, $config = [])
147147

148148
try {
149149
$documentMapping = $this->getDocumentReflectionMapping($documentReflection);
150+
if (!$documentMapping) {
151+
continue;
152+
}
150153
} catch (MissingDocumentAnnotationException $exception) {
151154
// Not a document, just ignore
152155
continue;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Unit\Mapping;
13+
14+
use ONGR\ElasticsearchBundle\Mapping\DocumentParser;
15+
use ONGR\ElasticsearchBundle\Tests\app\fixture\TestBundle\Document\LongDescriptionTrait;
16+
17+
class DocumentParserTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/*
20+
* @var \Doctrine\Common\Annotations\Reader
21+
*/
22+
private $reader;
23+
24+
/*
25+
* @var \ONGR\ElasticsearchBundle\Mapping\DocumentFinder
26+
*/
27+
private $finder;
28+
29+
public function setUp()
30+
{
31+
$this->reader = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
32+
->disableOriginalConstructor()
33+
->getMock();
34+
35+
$this->finder = $this->getMockBuilder('ONGR\ElasticsearchBundle\Mapping\DocumentFinder')
36+
->disableOriginalConstructor()
37+
->getMock();
38+
}
39+
40+
public function testReturnFalseOnTrait()
41+
{
42+
$traitReflection = new \ReflectionClass(
43+
'\\ONGR\\ElasticsearchBundle\\Tests\\app\\fixture\\TestBundle\\Document\\LongDescriptionTrait'
44+
);
45+
46+
$parser = new DocumentParser($this->reader, $this->finder);
47+
$this->assertFalse($parser->parse($traitReflection));
48+
}
49+
}

Tests/app/fixture/TestBundle/Document/CategoryObject.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class CategoryObject
3434
*/
3535
public $description;
3636

37+
/*
38+
* Test the use of traits
39+
*/
40+
use LongDescriptionTrait;
41+
3742
/**
3843
* @return string
3944
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\TestBundle\Document;
13+
14+
use ONGR\ElasticsearchBundle\Annotation as ES;
15+
16+
trait LongDescriptionTrait
17+
{
18+
/**
19+
* @ES\Property(type="text", name="long_description")
20+
* @var string
21+
*/
22+
private $long_description;
23+
24+
/**
25+
* @param string $long_description
26+
*/
27+
public function setLongDescription($long_description)
28+
{
29+
$this->long_description = $long_description;
30+
return $this;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getLongDescription()
37+
{
38+
return $this->long_description;
39+
}
40+
}

0 commit comments

Comments
 (0)