Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
abbadon1334 authored and StyleCIBot committed Jul 11, 2019
1 parent 8644f9c commit 4531199
Show file tree
Hide file tree
Showing 23 changed files with 248 additions and 378 deletions.
11 changes: 3 additions & 8 deletions example/test-example.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php
/**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -18,19 +16,16 @@
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

include __DIR__ . '/../vendor/autoload.php';
include __DIR__.'/../vendor/autoload.php';

use JuliusHaertl\PHPDocToRst\ApiDocBuilder;

$src = [__DIR__ . '/../src'];
$dst = __DIR__ . '/../tests/test-project-doc';
$src = [__DIR__.'/../src'];
$dst = __DIR__.'/../tests/test-project-doc';

$apiDocBuilder = new ApiDocBuilder($src, $dst);
$apiDocBuilder->setVerboseOutput(true);
$apiDocBuilder->setDebugOutput(true);
$apiDocBuilder->addExtension(\JuliusHaertl\PHPDocToRst\Extension\InterfaceImplementors::class);
$apiDocBuilder->build();

95 changes: 44 additions & 51 deletions src/ApiDocBuilder.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php
/**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -18,7 +16,6 @@
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace JuliusHaertl\PHPDocToRst;
Expand All @@ -45,15 +42,12 @@

/**
* This class is used to parse a project tree and generate rst files
* for all of the containing PHP structures
* for all of the containing PHP structures.
*
* Example usage is documented in examples/example.php
*
* @package JuliusHaertl\PHPDocToRst
*/
final class ApiDocBuilder
{

/** @var Project */
private $project;

Expand Down Expand Up @@ -96,11 +90,11 @@ final class ApiDocBuilder
public function __construct($srcDir, $dstDir)
{
$this->dstDir = $dstDir;
$this->srcDir = (array)$srcDir;
$this->srcDir = (array) $srcDir;
}

/**
* Run this to build the documentation
* Run this to build the documentation.
*/
public function build()
{
Expand All @@ -117,21 +111,21 @@ public function build()
*/
private function setupReflection()
{

$interfaceList = [];
$this->log('Start parsing files.');
foreach ($this->srcDir as $srcDir) {
$dir = new RecursiveDirectoryIterator($srcDir);
$dir = new RecursiveDirectoryIterator($srcDir);
$files = new RecursiveIteratorIterator($dir);

foreach ($files as $file) {
if ($file->isDir()) {
continue;
}

try {
$interfaceList[] = new LocalFile($file->getPathname());
} catch (Exception $e) {
$this->log('Failed to load ' . $file->getPathname() . PHP_EOL);
$this->log('Failed to load '.$file->getPathname().PHP_EOL);
}
}
}
Expand All @@ -142,54 +136,54 @@ private function setupReflection()
new Factory\Constant(new PrettyPrinter()),
new Factory\DocBlock(DocBlockFactory::createInstance()),
new Factory\File(NodesFactory::createInstance(), [
new ErrorHandlingMiddleware($this)
new ErrorHandlingMiddleware($this),
]),
new Factory\Function_(),
new Factory\Interface_(),
new Factory\Method(),
new Factory\Property(new PrettyPrinter()),
new Factory\Trait_(),
]);
$this->project = $projectFactory->create('MyProject', $interfaceList);
$this->project = $projectFactory->create('MyProject', $interfaceList);
$this->log('Successfully parsed files.');

// load extensions
foreach ($this->extensionNames as $extensionName) {
$extension = new $extensionName($this->project, $this->extensionArguments[$extensionName]);
if (!is_subclass_of($extension, Extension::class)) {
$this->log('Failed to load extension ' . $extensionName . '.');
$this->log('Failed to load extension '.$extensionName.'.');
}
$this->extensions[] = $extension;
$this->log('Extension ' . $extensionName . ' loaded.');
$this->log('Extension '.$extensionName.' loaded.');
}
}

/**
* Log a message
* Log a message.
*
* @param string $message Message to be logged
*/
public function log($message)
{
if ($this->verboseOutput) {
echo $message . PHP_EOL;
echo $message.PHP_EOL;
}
}

/**
* Create directory structure for the rst output
* Create directory structure for the rst output.
*
* @throws WriteException
*/
private function createDirectoryStructure()
{
foreach ($this->project->getNamespaces() as $namespace) {
$namespaceDir = $this->dstDir . str_replace('\\', '/', $namespace->getFqsen());
$namespaceDir = $this->dstDir.str_replace('\\', '/', $namespace->getFqsen());
if (is_dir($namespaceDir)) {
continue;
}
if (!mkdir($namespaceDir, 0755, true)) {
throw new WriteException('Could not create directory ' . $namespaceDir);
throw new WriteException('Could not create directory '.$namespaceDir);
}
}
}
Expand All @@ -202,49 +196,49 @@ private function parseFiles()
}
$this->log('Start building files');
foreach ($this->project->getFiles() as $file) {
/**
/*
* Go though interfaces/classes/functions of files and build documentation
*/
foreach ($file->getInterfaces() as $interface) {
$fqsen = $interface->getFqsen();
$builder = new InterfaceFileBuilder($file, $interface, $this->extensions);
$filename = $this->dstDir . str_replace('\\', '/', $fqsen) . '.rst';
$fqsen = $interface->getFqsen();
$builder = new InterfaceFileBuilder($file, $interface, $this->extensions);
$filename = $this->dstDir.str_replace('\\', '/', $fqsen).'.rst';
file_put_contents($filename, $builder->getContent());
$this->docFiles[(string)$interface->getFqsen()] = str_replace('\\', '/', $fqsen);
$this->docFiles[(string) $interface->getFqsen()] = str_replace('\\', '/', $fqsen);

// also build root namespace in indexes
if (strpos((string)substr($fqsen, 1), '\\') === false) {
if (strpos((string) substr($fqsen, 1), '\\') === false) {
$this->project->getRootNamespace()->addInterface($fqsen);
}
$this->debug('Written interface documentation to ' . $filename);
$this->debug('Written interface documentation to '.$filename);
}

foreach ($file->getClasses() as $class) {
$fqsen = $class->getFqsen();
$builder = new ClassFileBuilder($file, $class, $this->extensions);
$filename = $this->dstDir . str_replace('\\', '/', $fqsen) . '.rst';
$fqsen = $class->getFqsen();
$builder = new ClassFileBuilder($file, $class, $this->extensions);
$filename = $this->dstDir.str_replace('\\', '/', $fqsen).'.rst';
file_put_contents($filename, $builder->getContent());
$this->docFiles[(string)$class->getFqsen()] = str_replace('\\', '/', $fqsen);
$this->docFiles[(string) $class->getFqsen()] = str_replace('\\', '/', $fqsen);

// also build root namespace in indexes
if (strpos((string)substr($class->getFqsen(), 1), '\\') === false) {
if (strpos((string) substr($class->getFqsen(), 1), '\\') === false) {
$this->project->getRootNamespace()->addClass($fqsen);
}
$this->debug('Written class documentation to ' . $filename);
$this->debug('Written class documentation to '.$filename);
}

foreach ($file->getTraits() as $trait) {
$fqsen = $trait->getFqsen();
$builder = new TraitFileBuilder($file, $trait, $this->extensions);
$filename = $this->dstDir . str_replace('\\', '/', $fqsen) . '.rst';
$fqsen = $trait->getFqsen();
$builder = new TraitFileBuilder($file, $trait, $this->extensions);
$filename = $this->dstDir.str_replace('\\', '/', $fqsen).'.rst';
file_put_contents($filename, $builder->getContent());
$this->docFiles[(string)$trait->getFqsen()] = str_replace('\\', '/', $fqsen);
$this->docFiles[(string) $trait->getFqsen()] = str_replace('\\', '/', $fqsen);

// also build root namespace in indexes
if (strpos((string)substr($fqsen, 1), '\\') === false) {
if (strpos((string) substr($fqsen, 1), '\\') === false) {
$this->project->getRootNamespace()->addTrait($fqsen);
}
$this->debug('Written trait documentation to ' . $filename);
$this->debug('Written trait documentation to '.$filename);
}

// build array of functions per namespace
Expand All @@ -265,34 +259,33 @@ private function parseFiles()
}
$this->constants[$namespace][] = $constant;
}

}
}

/**
* Log a debug message
* Log a debug message.
*
* @param string $message Message to be logged
*/
public function debug($message)
{
if ($this->debugOutput) {
echo $message . PHP_EOL;
echo $message.PHP_EOL;
}
}

private function buildIndexes()
{
$this->log('Build indexes.');
$namespaces = $this->project->getNamespaces();
$namespaces = $this->project->getNamespaces();
$namespaces['\\'] = $this->project->getRootNamespace();
usort($namespaces, function (Namespace_ $a, Namespace_ $b) {
return strcmp($a->getFqsen(), $b->getFqsen());
});
/** @var Namespace_ $namespace */
foreach ($namespaces as $namespace) {
$fqsen = (string)$namespace->getFqsen();
$this->debug('Build namespace index for ' . $fqsen);
$fqsen = (string) $namespace->getFqsen();
$this->debug('Build namespace index for '.$fqsen);
$functions = [];
$constants = [];
if (array_key_exists($fqsen, $this->functions)) {
Expand All @@ -303,19 +296,19 @@ private function buildIndexes()
}
$builder = new NamespaceIndexBuilder($this->extensions, $namespaces, $namespace, $functions, $constants);
$builder->render();
$path = $this->dstDir . str_replace('\\', '/', $fqsen) . '/index.rst';
$path = $this->dstDir.str_replace('\\', '/', $fqsen).'/index.rst';
file_put_contents($path, $builder->getContent());
}

$this->log('Build main index files.');
$builder = new MainIndexBuilder($namespaces);
$builder->render();
$path = $this->dstDir . '/index-namespaces-all.rst';
$path = $this->dstDir.'/index-namespaces-all.rst';
file_put_contents($path, $builder->getContent());
}

/**
* Enable verbose logging output
* Enable verbose logging output.
*
* @param bool $v Set to true to enable
*/
Expand All @@ -325,7 +318,7 @@ public function setVerboseOutput($v)
}

/**
* Enable debug logging output
* Enable debug logging output.
*
* @param bool $v Set to true to enable
*/
Expand All @@ -341,7 +334,7 @@ public function setDebugOutput($v)
*/
public function addExtension($class, $arguments = [])
{
$this->extensionNames[] = $class;
$this->extensionNames[] = $class;
$this->extensionArguments[$class] = $arguments;
}
}
10 changes: 2 additions & 8 deletions src/Builder/ClassFileBuilder.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php
/**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -18,18 +16,15 @@
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace JuliusHaertl\PHPDocToRst\Builder;

use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\Php\Argument;
use phpDocumentor\Reflection\Php\Class_;

class ClassFileBuilder extends FileBuilder
{

protected function render()
{

Expand Down Expand Up @@ -63,11 +58,10 @@ protected function addImplementedInterfaces($element)
{
$implementedInterfaces = '';
foreach ($element->getInterfaces() as $int) {
$implementedInterfaces .= $this->getLink('interface', $int) . ' ';
$implementedInterfaces .= $this->getLink('interface', $int).' ';
}
if ($implementedInterfaces !== '') {
$this->addFieldList('Implements', $implementedInterfaces);
}
}

}
}
5 changes: 2 additions & 3 deletions src/Builder/ExtensionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Created by PhpStorm.
* User: jus
* Date: 11.10.17
* Time: 23:02
* Time: 23:02.
*/

namespace JuliusHaertl\PHPDocToRst\Builder;
Expand All @@ -12,7 +12,6 @@

trait ExtensionBuilder
{

/** @var Extension[] */
protected $extensions;

Expand All @@ -27,4 +26,4 @@ protected function callExtensions($type, $element)
$extension->render($type, $this, $element);
}
}
}
}
Loading

0 comments on commit 4531199

Please sign in to comment.