Skip to content

Commit

Permalink
More phpstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Sep 10, 2024
1 parent 4266f4e commit a78629f
Show file tree
Hide file tree
Showing 26 changed files with 130 additions and 74 deletions.
3 changes: 2 additions & 1 deletion src/Autoloaders/MultiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace donatj\MDDoc\Autoloaders;

use Countable;
use donatj\MDDoc\Autoloaders\Interfaces\AutoloaderInterface;

/**
* A simple autoloader chain
*/
class MultiLoader implements AutoloaderInterface, \Countable {
class MultiLoader implements AutoloaderInterface, Countable {

/** @var AutoloaderInterface[] */
private $loaders;
Expand Down
3 changes: 1 addition & 2 deletions src/Autoloaders/Psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Psr4 implements AutoloaderInterface {

/**
* @param string $root_namespace Namespace prefix
* @param string $path Root path
* @param string $path Root path
*/
public function __construct( string $root_namespace, string $path ) {
$this->namespace = $this->trimSlashes($root_namespace);
Expand All @@ -43,7 +43,6 @@ public function __invoke( string $className ) : ?string {
if( file_exists($filename) ) {
return $filename;
}

}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Documentation/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class AbstractElement implements Interfaces\ElementInterface {

public function __construct( ImmutableAttributeTree $attributeTree, string $textContent = '' ) {
$this->attributeTree = $attributeTree;
$this->textContent = $textContent;
$this->textContent = $textContent;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Documentation/AbstractNestedDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getChildren() : array {
* @return \donatj\MDDoc\Documentation\Interfaces\DocumentationInterface[]
*/
public function getDocumentationChildren() : array {
$return = [];
$return = [];
$children = $this->getChildren();

foreach( $children as $child ) {
Expand Down
4 changes: 2 additions & 2 deletions src/Documentation/Badges/BadgeGitHubActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class BadgeGitHubActions extends Badge {
*/
public const OPT_NAME = 'name';
/** The name of the branch to show the badge for. Defaults to the default branch. */
public const OPT_BRANCH = 'branch';
public const OPT_EVENT = 'event'; // @todo - this seems to be broken?
public const OPT_BRANCH = 'branch';
public const OPT_EVENT = 'event'; // @todo - this seems to be broken?
/**
* The filename of the workflow file to use as the badge source
*
Expand Down
6 changes: 4 additions & 2 deletions src/Documentation/Badges/BadgePoser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class BadgePoser extends Badge {
private const URL_PACKAGIST_BASE = 'https://packagist.org/packages/';

/**
* The type of badge to display. One of: "version" "downloads" "unstable" "license" "monthly" "daily" "phpversion" "composerlock"
* The type of badge to display. One of: "version" "downloads" "unstable" "license" "monthly" "daily" "phpversion"
* "composerlock"
*
* @mddoc-required
*/
public const OPT_TYPE = 'type';
Expand Down Expand Up @@ -76,7 +78,7 @@ protected function init() : void {
}

$parsed = @json_decode($data, true);
if( !empty($parsed['name']) && is_string($parsed['name']) ) {
if( is_array($parsed) && !empty($parsed['name']) && is_string($parsed['name']) ) {
$name = $parsed['name'];
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Documentation/Badges/BadgeScrutinizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ class BadgeScrutinizer extends Badge {
private const URL_SCRUTINIZER_BASE = 'https://scrutinizer-ci.com/g/';

/**
* The packagist name of the Scrutinizer Project. Defaults to the name key of the composer.json file in the root of the project. Required if the composer.json file is not present.
* The packagist name of the Scrutinizer Project. Defaults to the name key of the composer.json file in the root of
* the project. Required if the composer.json file is not present.
*
* @mddoc-required
*/
public const OPT_NAME = 'name';
/**
* The type of badge to display. One of: "quality" "coverage" "build-status"
*
* @mddoc-required
*/
public const OPT_TYPE = 'type';
Expand Down
14 changes: 7 additions & 7 deletions src/Documentation/Badges/BadgeShielded.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BadgeShielded extends Badge {
/** The title of the badge when displaying a static badge. */
public const OPT_TITLE = 'title';
/** The text of the badge when displaying a static badge. */
public const OPT_TEXT = 'text';
public const OPT_TEXT = 'text';

protected function init() : void {
$id = $this->getOption(self::OPT_ID);
Expand All @@ -36,12 +36,12 @@ protected function init() : void {
$color = $this->getOption(self::OPT_COLOR) ?? '';

$url .= '?' . http_build_query(array_filter([
'title' => $title,
'text' => $text,
'color' => $color,
], static function ( $v ) {
return $v !== '';
}));
'title' => $title,
'text' => $text,
'color' => $color,
], static function( $v ) {
return $v !== '';
}));

$url = rtrim($url, '?');

Expand Down
6 changes: 4 additions & 2 deletions src/Documentation/Badges/BadgeTravis.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
class BadgeTravis extends Badge {

/**
* The packagist name of the Travis Project. Defaults to the name key of the composer.json file in the root of the project. Required if the composer.json file is not present.
* The packagist name of the Travis Project. Defaults to the name key of the composer.json file in the root of the
* project. Required if the composer.json file is not present.
*
* @mddoc-required
*/
public const OPT_NAME = 'name';
public const OPT_NAME = 'name';
private const URL_TRAVIS_BASE = 'https://travis-ci.org/';

/** The branch to show. Defaults to "master" */
Expand Down
21 changes: 11 additions & 10 deletions src/Documentation/ComposerInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use donatj\MDDoc\Exceptions\PathNotReadableException;
use donatj\MDDom\CodeBlock;
use donatj\MDDom\Paragraph;
use RuntimeException;

class ComposerInstall extends AbstractDocPart {

Expand Down Expand Up @@ -37,8 +38,8 @@ public function output( int $depth ) : Paragraph {
$composerName = null;

try {
$file = $this->getWorkingFilePath('composer.json');
$data = @file_get_contents($file);
$file = $this->getWorkingFilePath('composer.json');
$data = @file_get_contents($file);
if( $data === false ) {
throw new PathNotReadableException('Unable to read composer.json', $file);
}
Expand All @@ -47,26 +48,26 @@ public function output( int $depth ) : Paragraph {
if( is_array($parsed) && !empty($parsed['name']) ) {
$composerName = $parsed['name'];
}
}catch(PathNotReadableException $e) {
} catch( PathNotReadableException $e ) {
// ignore
}

if($packageNames) {
foreach($packageNames as $key => $packageName) {
if( $packageNames ) {
foreach( $packageNames as $key => $packageName ) {
if( $packageName === '.' ) {
if(!$composerName) {
throw new \RuntimeException('Unable to determine composer package name from composer.json');
if( !$composerName ) {
throw new RuntimeException('Unable to determine composer package name from composer.json');
}

$packageNames[$key] = $composerName;
}
}
}elseif( $composerName ) {
} elseif( $composerName ) {
$packageNames = [ $composerName ];
}

if(!$packageNames) {
throw new \RuntimeException('Unable to determine composer package name from composer.json and no package names provided');
if( !$packageNames ) {
throw new RuntimeException('Unable to determine composer package name from composer.json and no package names provided');
}

$para = new Paragraph;
Expand Down
2 changes: 1 addition & 1 deletion src/Documentation/ComposerRequires.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function output( int $depth ) : Paragraph {
$parsed = @json_decode($data, true);

$para = new Paragraph;
if( isset($parsed['require']) && is_array($parsed['require']) ) {
if( is_array($parsed) && isset($parsed['require']) && is_array($parsed['require']) ) {
foreach( $parsed['require'] as $field => $version ) {
$para->appendChild(new \donatj\MDDom\Text("- **$field**: $version\n"));
}
Expand Down
8 changes: 7 additions & 1 deletion src/Documentation/DocPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace donatj\MDDoc\Documentation;

use donatj\MDDoc\Exceptions\ConfigException;
use donatj\MDDoc\Exceptions\TargetNotWritableException;
use donatj\MDDom\Document;
use Psr\Log\LoggerAwareInterface;
Expand Down Expand Up @@ -49,7 +50,12 @@ public function output( int $depth ) : string {
}

foreach( $this->getDocumentationChildren() as $child ) {
$document->appendChild($child->output(0));
$output = $child->output(0);
if( $output === null ) {
throw new ConfigException(get_class($child) . ' incorrectly used as a nested element');
}

$document->appendChild($output);
}

if( @file_put_contents($target, $document->exportMarkdown(-1)) === false ) {
Expand Down
4 changes: 3 additions & 1 deletion src/Documentation/ExecOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
use donatj\MDDom\Code;
use donatj\MDDom\CodeBlock;
use donatj\MDDom\Paragraph;
use RuntimeException;

class ExecOutput extends AbstractDocPart {

/**
* The command to execute
*
* @mddoc-required
*/
public const OPT_CMD = 'cmd';
Expand Down Expand Up @@ -71,7 +73,7 @@ public function output( int $depth ) : AbstractElement {
case self::FORMAT_CODE_BLOCK:
return new CodeBlock($md, $this->getOption('lang'));
default:
throw new \RuntimeException("unhandled format '$format'");
throw new RuntimeException("unhandled format '$format'");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Documentation/Interfaces/DocumentationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
interface DocumentationInterface extends ElementInterface {

/**
* @return AbstractElement|string Cannot be annotated as also accepts __toString-able objects
* @return AbstractElement|string|void Cannot be annotated as also accepts __toString-able objects
*/
public function output( int $depth );

Expand Down
14 changes: 11 additions & 3 deletions src/Documentation/PhpFileDocs.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use phpDocumentor\Reflection\Php\Method;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use RuntimeException;

class PhpFileDocs extends AbstractDocPart implements AutoloaderAware, LoggerAwareInterface {

Expand Down Expand Up @@ -72,7 +73,7 @@ class PhpFileDocs extends AbstractDocPart implements AutoloaderAware, LoggerAwar
* @return AbstractElement|string
*/
public function output( int $depth ) {
$file = $this->getOption(self::OPT_NAME);
$file = $this->requireOption(self::OPT_NAME);
$path = $this->getWorkingFilePath($file);

return $this->scanSourceFile($path, $depth);
Expand Down Expand Up @@ -185,6 +186,9 @@ private function scanSourceFile( string $filename, int $depth ) {
$constantData = $reflector->getConstants();
foreach( $constantData as $constants ) {
$constant = reset($constants);
if( !$constant ) {
continue;
}

$visibility = (string)$constant->getVisibility();
if( $visibility === 'private' ) {
Expand Down Expand Up @@ -468,7 +472,11 @@ private function scanSourceFile( string $filename, int $depth ) {
return $document;
}

private function shouldSkip( DocBlock $block ) : bool {
private function shouldSkip( ?DocBlock $block ) : bool {
if( !$block ) {
return false;
}

if( $access = $block->getTagsByName('access') ) {
$access = reset($access);
if( !$access instanceof Generic ) {
Expand Down Expand Up @@ -681,7 +689,7 @@ private function logInvalidTag(
if( $this->logger ) {
$this->logger->notice($message, $ctx);
} else {
throw new \RuntimeException($message);
throw new RuntimeException($message);
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/Documentation/RecursiveDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

namespace donatj\MDDoc\Documentation;

use ArrayIterator;
use donatj\MDDoc\Autoloaders\Interfaces\AutoloaderInterface;
use donatj\MDDoc\Documentation\Interfaces\AutoloaderAware;
use donatj\MDDom\Document;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
use SplFileInfo;

class RecursiveDirectory extends AbstractNestedDoc implements AutoloaderAware, LoggerAwareInterface {

Expand Down Expand Up @@ -79,21 +84,21 @@ private function getFileList( string $path ) : iterable {
$path = rtrim($path, DIRECTORY_SEPARATOR);

if( is_dir($path) ) {
$dir = new \RecursiveDirectoryIterator($path);
$ite = new \RecursiveIteratorIterator($dir);
$dir = new RecursiveDirectoryIterator($path);
$ite = new RecursiveIteratorIterator($dir);
/** @var \Traversable<\SplFileInfo> $files */
$files = new \RegexIterator($ite, "/\\.php$/");
$files = new RegexIterator($ite, "/\\.php$/");

$fileArray = iterator_to_array($files, false);
usort($fileArray, function ( \SplFileInfo $a, \SplFileInfo $b ) : int {
usort($fileArray, function( SplFileInfo $a, SplFileInfo $b ) : int {
return strnatcasecmp($a->getRealPath(), $b->getRealPath());
});

return new \ArrayIterator($fileArray);
return new ArrayIterator($fileArray);
}

return new \ArrayIterator([
new \SplFileInfo($path),
return new ArrayIterator([
new SplFileInfo($path),
]);
}

Expand Down
13 changes: 9 additions & 4 deletions src/Documentation/Replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,30 @@ class Replace extends AbstractNestedDoc {
public const OPT_REGEX = 'regex';

public function output( int $depth ) : string {
$regex = $this->getOption(self::OPT_REGEX);
/** @var "true"|"false" $regex */
$regex = $this->requireOption(self::OPT_REGEX);
$regex = strtolower($regex);

$output = '';
foreach( $this->getDocumentationChildren() as $child ) {
$result = $child->output($depth);
if( $result === null ) {
throw new ConfigException(get_class($child) . ' incorrectly used as a nested element');
}

if( is_string($result) ) {
$output .= $result;
} else {
$output .= $result->exportMarkdown($depth);
}
}

$search = $this->getOption(self::OPT_SEARCH);
$replace = $this->getOption(self::OPT_REPLACE);
$search = $this->requireOption(self::OPT_SEARCH);
$replace = $this->requireOption(self::OPT_REPLACE);

if( $regex === 'true' ) {
$output = @preg_replace($search, $replace, $output);
if( preg_last_error() !== PREG_NO_ERROR ) {
if( $output === null || preg_last_error() !== PREG_NO_ERROR ) {
throw new ConfigException("user regex error: " . preg_last_error_msg());
}

Expand Down
Loading

0 comments on commit a78629f

Please sign in to comment.