This repository was archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Add ability to auto-document complex types #53
Merged
weierophinney
merged 8 commits into
zendframework:develop
from
kynx:feature/complextype-documentation
Apr 12, 2018
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3b7a48b
Add ability to auto-document complex types
mattkynaston 1a2de31
Fixed file header
mattkynaston 008ff6e
Fixed coverage
mattkynaston 628de8f
Fixed namespace for complex type documentation, added test to ensure …
mattkynaston bf84168
Removed return types
mattkynaston d84e527
Removed anonymous classes from tests + CS fixes
mattkynaston afad732
Grrr... missed a return type
mattkynaston 011e032
Fixed scalar type declaration (is it sooo long since I coded in 5.6?)
mattkynaston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Wsdl/DocumentationStrategy/DocumentationStrategyInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Soap\Wsdl\DocumentationStrategy; | ||
|
||
use ReflectionClass; | ||
use ReflectionProperty; | ||
|
||
/** | ||
* Implement this interface to provide contents for <wsdl:documentation> elements on complex types | ||
*/ | ||
interface DocumentationStrategyInterface | ||
{ | ||
/** | ||
* Returns documentation for complex type property | ||
* | ||
* @param ReflectionProperty $property | ||
* @return string | ||
*/ | ||
public function getPropertyDocumentation(ReflectionProperty $property) : string; | ||
|
||
/** | ||
* Returns documentation for complex type | ||
* | ||
* @param ReflectionClass $class | ||
* @return string | ||
*/ | ||
public function getComplexTypeDocumentation(ReflectionClass $class) : string; | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Wsdl/DocumentationStrategy/ReflectionDocumentation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Soap\Wsdl\DocumentationStrategy; | ||
|
||
use ReflectionClass; | ||
use ReflectionProperty; | ||
|
||
final class ReflectionDocumentation implements DocumentationStrategyInterface | ||
{ | ||
public function getPropertyDocumentation(ReflectionProperty $property) : string | ||
{ | ||
return $this->parseDocComment($property->getDocComment()); | ||
} | ||
|
||
public function getComplexTypeDocumentation(ReflectionClass $class) : string | ||
{ | ||
return $this->parseDocComment($class->getDocComment()); | ||
} | ||
|
||
private function parseDocComment(string $docComment) : string | ||
{ | ||
$documentation = []; | ||
foreach (explode("\n", $docComment) as $i => $line) { | ||
if ($i == 0) { | ||
continue; | ||
} | ||
|
||
$line = trim(preg_replace('/\s*\*+/', '', $line)); | ||
if (preg_match('/^(@[a-z]|\/)/i', $line)) { | ||
break; | ||
} | ||
|
||
// only include newlines if we've already got documentation | ||
if (! empty($documentation) || $line != '') { | ||
$documentation[] = $line; | ||
} | ||
} | ||
|
||
return join("\n", $documentation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
test/Wsdl/DocumentationStrategy/ReflectionDocumentationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Soap\Wsdl\DocumentationStrategy; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ReflectionClass; | ||
use Zend\Soap\Wsdl\DocumentationStrategy\ReflectionDocumentation; | ||
use ZendTest\Soap\TestAsset\WsdlTestClass; | ||
|
||
class ReflectionDocumentationTest extends TestCase | ||
{ | ||
/** | ||
* @var ReflectionDocumentation | ||
*/ | ||
private $documentation; | ||
|
||
protected function setUp() | ||
{ | ||
$this->documentation = new ReflectionDocumentation(); | ||
} | ||
|
||
public function testGetPropertyDocumentationParsesDocComment() | ||
{ | ||
$class = new class { | ||
/** | ||
* Property documentation | ||
*/ | ||
public $foo; | ||
}; | ||
|
||
$reflection = new ReflectionClass($class); | ||
$actual = $this->documentation->getPropertyDocumentation($reflection->getProperty('foo')); | ||
$this->assertEquals('Property documentation', $actual); | ||
} | ||
|
||
public function testGetPropertyDocumentationSkipsAnnotations() | ||
{ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to run via phpcs when done; extra lines like this one should be picked up and flagged. |
||
$class = new class { | ||
/** | ||
* Property documentation | ||
* @type int | ||
*/ | ||
public $foo; | ||
}; | ||
|
||
$reflection = new ReflectionClass($class); | ||
$actual = $this->documentation->getPropertyDocumentation($reflection->getProperty('foo')); | ||
$this->assertEquals('Property documentation', $actual); | ||
} | ||
|
||
public function testGetPropertyDocumentationReturnsEmptyString() | ||
{ | ||
|
||
$class = new class { | ||
public $foo; | ||
}; | ||
|
||
$reflection = new ReflectionClass($class); | ||
$actual = $this->documentation->getPropertyDocumentation($reflection->getProperty('foo')); | ||
$this->assertEquals('', $actual); | ||
} | ||
|
||
public function getGetComplexTypeDocumentationParsesDocComment() | ||
{ | ||
$reflection = new ReflectionClass(new WsdlTestClass()); | ||
$actual = $this->documentation->getComplexTypeDocumentation($reflection); | ||
$this->assertEquals('Test class', $actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -573,6 +573,20 @@ public function testAddDocumentationToSetInsertsBefore() | |
$this->assertEquals('documentation', $nodes->item(0)->nodeName); | ||
} | ||
|
||
public function testComplexTypeDocumentationAddedAsAnnotation() | ||
{ | ||
$this->wsdl->addComplexType('\ZendTest\Soap\TestAsset\WsdlTestClass'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import the class and use |
||
$nodes = $this->xpath->query('//xsd:complexType[@name="WsdlTestClass"]'); | ||
|
||
$this->wsdl->addDocumentation($nodes->item(0), 'documentation'); | ||
|
||
$nodes = $this->xpath->query('//xsd:complexType[@name="WsdlTestClass"]/*[1]'); | ||
$this->assertEquals('xsd:annotation', $nodes->item(0)->nodeName); | ||
|
||
$nodes = $this->xpath->query('//xsd:complexType[@name="WsdlTestClass"]/xsd:annotation/*[1]'); | ||
$this->assertEquals('xsd:documentation', $nodes->item(0)->nodeName); | ||
} | ||
|
||
public function testDumpToFile() | ||
{ | ||
$file = tempnam(sys_get_temp_dir(), 'zfunittest'); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you remove the scalar type hints and return type hints, you'll also need to update this test to remove anonymous class usage. Create classes under a
TestAsset
directory, and instantiate them here.