Skip to content

Commit

Permalink
Merge branch 'phpstan'
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Nov 21, 2023
2 parents cccf596 + 494a190 commit 0a2aec1
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![PHPUnit ](.github/coverage.svg)](https://brianhenryie.github.io/strauss/)
[![PHPUnit ](.github/coverage.svg)](https://brianhenryie.github.io/strauss/) [![PHPStan ](https://img.shields.io/badge/PHPStan-Level%207-2a5ea7.svg)](https://phpstan.org/)

# Strauss

Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: 7
paths:
- src
bootstrapFiles:
- phpstanbootstrap.php
ignoreErrors:
-
message: '#Property.*excludePackages is unused#'
path: src/Composer/Extra/StraussConfig.php
9 changes: 9 additions & 0 deletions phpstanbootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Define constants that PhpStan cannot find.
*
* @see https://phpstan.org/user-guide/discovering-symbols#global-constants
*
* @package brianhenryie/strauss
*/

22 changes: 8 additions & 14 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class Autoload
* The files autolaoders of packages that have been copied by Strauss.
* Keyed by package path.
*
* @var array
* @var array<string, array<string>> $discoveredFilesAutoloaders Array of packagePath => array of relativeFilePaths.
*/
protected array $discoveredFilesAutoloaders;

/**
* Autoload constructor.
* @param StraussConfig $config
* @param string $workingDir
* @param array<string, array<string>> $files
* @param array<string, array<string>> $discoveredFilesAutoloaders
*/
public function __construct(StraussConfig $config, string $workingDir, array $discoveredFilesAutoloaders)
{
Expand All @@ -44,7 +44,7 @@ public function __construct(StraussConfig $config, string $workingDir, array $di
$this->filesystem = new Filesystem(new Local($workingDir));
}

public function generate()
public function generate(): void
{
// Do not overwrite Composer's autoload.php.
// The correct solution is to add "classmap": ["vendor"] to composer.json, then run composer dump-autoload.
Expand Down Expand Up @@ -73,7 +73,7 @@ public function generate()
* @see ClassMapGenerator::dump()
*
*/
protected function generateClassmap(): string
protected function generateClassmap(): void
{

// Hyphen used to match WordPress Coding Standards.
Expand All @@ -87,20 +87,16 @@ protected function generateClassmap(): string
$targetDirectory
);

$dirname = '';

foreach ($dirs as $dir) {
if (!is_dir($dir)) {
continue;
}

$dirMap = ClassMapGenerator::createMap($dir);

$dirname = preg_replace('/[^a-z]/i', '', str_replace(getcwd(), '', $dir));

array_walk(
$dirMap,
function (&$filepath, $_class) use ($dir, $dirname) {
function (&$filepath, $_class) use ($dir) {
$filepath = "\$strauss_src . '"
. DIRECTORY_SEPARATOR
. ltrim(str_replace($dir, '', $filepath), DIRECTORY_SEPARATOR) . "'";
Expand All @@ -122,11 +118,9 @@ function (&$filepath, $_class) use ($dir, $dirname) {

file_put_contents($dir . $output_filename, ob_get_clean());
}

return $dirname;
}

protected function generateFilesAutoloader()
protected function generateFilesAutoloader(): void
{

// Hyphen used to match WordPress Coding Standards.
Expand All @@ -142,7 +136,7 @@ protected function generateFilesAutoloader()
. DIRECTORY_SEPARATOR
. ltrim($this->config->getTargetDirectory(), DIRECTORY_SEPARATOR);

$dirname = preg_replace('/[^a-z]/i', '', str_replace(getcwd(), '', $targetDirectory));
// $dirname = preg_replace('/[^a-z]/i', '', str_replace(getcwd(), '', $targetDirectory));

ob_start();

Expand All @@ -166,7 +160,7 @@ protected function generateFilesAutoloader()
file_put_contents($targetDirectory . $outputFilename, ob_get_clean());
}

protected function generateAutoloadPhp()
protected function generateAutoloadPhp(): void
{

$autoloadPhp = <<<'EOD'
Expand Down
11 changes: 9 additions & 2 deletions src/ChangeEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ class ChangeEnumerator
* @var string[]
*/
protected array $excludePackagesFromPrefixing = array();

/** @var string[] */
protected array $excludeNamespacesFromPrefixing = array();

/** @var string[] */
protected array $excludeFilePatternsFromPrefixing = array();

/** @var string[] */
protected array $namespaceReplacementPatterns = array();

/** @var string[] */
Expand Down Expand Up @@ -88,7 +92,7 @@ public function getDiscoveredConstants(): array
* @param string $absoluteTargetDir
* @param array<string,array{dependency:ComposerPackage,sourceAbsoluteFilepath:string,targetRelativeFilepath:string}> $filesArray
*/
public function findInFiles($absoluteTargetDir, $filesArray)
public function findInFiles($absoluteTargetDir, $filesArray): void
{
// $relativeFilepaths = array_keys( $filesArray );

Expand All @@ -113,6 +117,9 @@ public function findInFiles($absoluteTargetDir, $filesArray)
// $contents = $this->filesystem->read($targetFile);

$contents = file_get_contents($filepath);
if (false === $contents) {
throw new \Exception("Failed to read file at {$filepath}");
}

$this->find($contents);
}
Expand Down Expand Up @@ -193,7 +200,7 @@ function ($matches) {
);
}

protected function addDiscoveredNamespaceChange(string $namespace)
protected function addDiscoveredNamespaceChange(string $namespace): void
{

foreach ($this->excludeNamespacesFromPrefixing as $excludeNamespace) {
Expand Down
4 changes: 2 additions & 2 deletions src/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function __construct(StraussConfig $config, string $workingDir)
* Maybe delete the source files that were copied (depending on config),
* then delete empty directories.
*
* @param array $sourceFiles
* @param string[] $sourceFiles Relative filepaths.
*/
public function cleanup(array $sourceFiles)
public function cleanup(array $sourceFiles): void
{
if (!$this->isDeleteVendorPackages && !$this->isDeleteVendorFiles) {
return;
Expand Down
21 changes: 11 additions & 10 deletions src/Composer/ComposerPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Composer\Composer;
use Composer\Factory;
use Composer\IO\NullIO;
use stdClass;

/**
* @phpstan-type AutoloadKey array{files?:array<string>,classmap?:array<string>,"psr-4"?:array<string,string|array<string>>}
*/
class ComposerPackage
{
/**
Expand Down Expand Up @@ -50,14 +52,14 @@ class ComposerPackage
/**
* The discovered files, classmap, psr0 and psr4 autoload keys discovered (as parsed by Composer).
*
* @var array<string, array<string, string>>
* @var AutoloadKey
*/
protected array $autoload = [];

/**
* The names in the composer.json's "requires" field (without versions).
*
* @var array
* @var string[]
*/
protected array $requiresNames = [];

Expand All @@ -67,7 +69,7 @@ class ComposerPackage
* @param string $absolutePath The absolute path to the vendor folder with the composer.json "name",
* i.e. the domain/package definition, which is the vendor subdir from where the package's
* composer.json should be read.
* @param ?array $overrideAutoload Optional configuration to replace the package's own autoload definition with
* @param ?array{files?:array<string>, classmap?:array<string>, psr?:array<string,string|array<string>>} $overrideAutoload Optional configuration to replace the package's own autoload definition with
* another which Strauss can use.
* @return ComposerPackage
*/
Expand All @@ -83,9 +85,8 @@ public static function fromFile(string $absolutePath, array $overrideAutoload =
}

/**
* @param array $jsonArray composer.json decoded to array
* @param ?array $overrideAutoload
* @return ComposerPackage
* @param array{name?:string, license?:string, requires?:array<string,string>, autoload?:AutoloadKey} $jsonArray composer.json decoded to array
* @param ?AutoloadKey $overrideAutoload New autoload rules to replace the existing ones.
*/
public static function fromComposerJsonArray($jsonArray, array $overrideAutoload = null): ComposerPackage
{
Expand All @@ -99,8 +100,8 @@ public static function fromComposerJsonArray($jsonArray, array $overrideAutoload
/**
* Create a PHP object to represent a composer package.
*
* @param array $overrideAutoload Optional configuration to replace the package's own autoload definition with
* another which Strauss can use.
* @param Composer $composer
* @param ?AutoloadKey $overrideAutoload Optional configuration to replace the package's own autoload definition with another which Strauss can use.
*/
public function __construct(Composer $composer, array $overrideAutoload = null)
{
Expand Down Expand Up @@ -165,7 +166,7 @@ public function getPackageAbsolutePath(): ?string
* e.g. ['psr-4' => [ 'BrianHenryIE\Project' => 'src' ]]
* e.g. ['psr-4' => [ 'BrianHenryIE\Project' => ['src','lib] ]]
*
* @return array<string, array<int|string, string|array<string>>>
* @return AutoloadKey
*/
public function getAutoload(): array
{
Expand Down
Loading

0 comments on commit 0a2aec1

Please sign in to comment.