diff --git a/src/ChangeEnumerator.php b/src/ChangeEnumerator.php index 52ba84b8..3719978f 100644 --- a/src/ChangeEnumerator.php +++ b/src/ChangeEnumerator.php @@ -19,7 +19,7 @@ public function __construct(StraussConfig $config, string $workingDir) $absoluteTargetDir = $workingDir . $config->getTargetDirectory(); } - public function determineReplacements(DiscoveredSymbols $discoveredSymbols) + public function determineReplacements(DiscoveredSymbols $discoveredSymbols): void { foreach ($discoveredSymbols->getSymbols() as $symbol) { if (in_array( diff --git a/src/Copier.php b/src/Copier.php index 2c473d7f..26e45b38 100644 --- a/src/Copier.php +++ b/src/Copier.php @@ -72,15 +72,9 @@ public function prepareTarget(): void } } - - /** - * - */ public function copy(): void { - /** - * @var string $targetRelativeFilepath * @var File $file */ foreach ($this->files->getFiles() as $file) { diff --git a/src/DependenciesEnumerator.php b/src/DependenciesEnumerator.php index a3816a73..b3da77e1 100644 --- a/src/DependenciesEnumerator.php +++ b/src/DependenciesEnumerator.php @@ -50,9 +50,9 @@ class DependenciesEnumerator protected array $filesAutoloaders = []; /** - * @param ?array{files?:array, classmap?:array, psr?:array>} $overrideAutoload + * @var array{}|array,classmap?:array,"psr-4":array>}> $overrideAutoload */ - protected array $overrideAutoload; + protected array $overrideAutoload = array(); /** * Constructor. @@ -72,6 +72,10 @@ public function __construct( $this->filesystem = new Filesystem(new LocalFilesystemAdapter($this->workingDir)); } + /** + * @return array Packages indexed by package name. + * @throws Exception + */ public function getAllDependencies(): array { $this->recursiveGetAllDependencies($this->requiredPackageNames); diff --git a/src/DiscoveredFiles.php b/src/DiscoveredFiles.php index 9d910df7..6d96d43b 100644 --- a/src/DiscoveredFiles.php +++ b/src/DiscoveredFiles.php @@ -5,16 +5,15 @@ use ArrayAccess; use BrianHenryIE\Strauss\Composer\ComposerPackage; -class DiscoveredFiles implements ArrayAccess +class DiscoveredFiles { - /** @var array */ protected array $files = []; /** * @param File $file */ - public function add(File $file) + public function add(File $file): void { $this->files[$file->getTargetRelativePath()] = $file; } @@ -27,47 +26,6 @@ public function getFiles(): array return $this->files; } - /** - * @return bool - */ - #[\ReturnTypeWillChange] - public function offsetExists($offset) - { - return isset($this->files[$offset]); - } - - /** - * @return File - */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) - { - return $this->files[$offset]; - } - - /** - * @inheritDoc - */ - #[\ReturnTypeWillChange] - public function offsetSet($offset, $value) - { - if (is_null($offset)) { - $this->files[] = $value; - } else { - $this->files[$offset] = $value; - } - } - - /** - * @inheritDoc - */ - #[\ReturnTypeWillChange] - public function offsetUnset($offset) - { - unset($this->files[$offset]); - } - - /** * Returns all found files. * diff --git a/src/DiscoveredSymbol.php b/src/DiscoveredSymbol.php index faa79938..07b57aec 100644 --- a/src/DiscoveredSymbol.php +++ b/src/DiscoveredSymbol.php @@ -13,6 +13,10 @@ abstract class DiscoveredSymbol protected string $replacement; + /** + * @param string $symbol The classname / namespace etc. + * @param File $file The file it was discovered in. + */ public function __construct(string $symbol, File $file) { $this->symbol = $symbol; diff --git a/src/DiscoveredSymbols.php b/src/DiscoveredSymbols.php index 88d543db..b1c22df3 100644 --- a/src/DiscoveredSymbols.php +++ b/src/DiscoveredSymbols.php @@ -2,15 +2,17 @@ namespace BrianHenryIE\Strauss; -use ArrayAccess; use BrianHenryIE\Strauss\Types\ClassSymbol; use BrianHenryIE\Strauss\Types\ConstantSymbol; use BrianHenryIE\Strauss\Types\NamespaceSymbol; -class DiscoveredSymbols implements ArrayAccess +class DiscoveredSymbols { - - /** @var array */ + /** + * All discovered symbols, grouped by type, indexed by original name. + * + * @var array> + */ protected array $types = []; public function __construct() @@ -25,9 +27,9 @@ public function __construct() /** * @param DiscoveredSymbol $symbol */ - public function add(DiscoveredSymbol $symbol) + public function add(DiscoveredSymbol $symbol): void { - $this->types[ get_class($symbol)][$symbol->getOriginalSymbol() ] = $symbol; + $this->types[get_class($symbol)][$symbol->getOriginalSymbol()] = $symbol; } /** @@ -42,46 +44,6 @@ public function getSymbols(): array ); } - /** - * @return bool - */ - #[\ReturnTypeWillChange] - public function offsetExists($offset) - { - return isset($this->types[ $offset ]); - } - - /** - * @return DiscoveredSymbol - */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) - { - return $this->types[ $offset ]; - } - - /** - * @inheritDoc - */ - #[\ReturnTypeWillChange] - public function offsetSet($offset, $value) - { - if (is_null($offset)) { - $this->types[] = $value; - } else { - $this->types[ $offset ] = $value; - } - } - - /** - * @inheritDoc - */ - #[\ReturnTypeWillChange] - public function offsetUnset($offset) - { - unset($this->types[ $offset ]); - } - /** * @return array */ @@ -98,7 +60,10 @@ public function getNamespaces(): array return $this->types[NamespaceSymbol::class]; } - public function getClasses() + /** + * @return array + */ + public function getClasses(): array { return $this->types[ClassSymbol::class]; } diff --git a/src/File.php b/src/File.php index 963f7105..1eb3f00a 100644 --- a/src/File.php +++ b/src/File.php @@ -46,12 +46,12 @@ public function __construct(ComposerPackage $dependency, string $packageRelative $this->sourceAbsolutePath = $sourceAbsolutePath; } - public function getDependency() + public function getDependency(): ComposerPackage { return $this->dependency; } - public function getSourcePath(string $relativeTo = '') + public function getSourcePath(string $relativeTo = ''): string { return str_replace($relativeTo, '', $this->sourceAbsolutePath); } @@ -137,12 +137,12 @@ public function isFilesAutoloaderFile(): bool return in_array('files', $this->autoloaderTypes, true); } - public function addDiscoveredSymbol(DiscoveredSymbol $symbol) + public function addDiscoveredSymbol(DiscoveredSymbol $symbol): void { $this->discoveredSymbols[$symbol->getOriginalSymbol()] = $symbol; } - public function getContents() + public function getContents(): string { // TODO: use flysystem diff --git a/src/FileEnumerator.php b/src/FileEnumerator.php index d46e8462..a16611fa 100644 --- a/src/FileEnumerator.php +++ b/src/FileEnumerator.php @@ -156,9 +156,16 @@ public function compileFileList(): DiscoveredFiles return $this->discoveredFiles; } - protected function addFile(ComposerPackage $dependency, string $packageRelativePath, string $autoloaderType) + /** + * @uses \BrianHenryIE\Strauss\DiscoveredFiles::add() + * + * @param ComposerPackage $dependency + * @param string $packageRelativePath + * @param string $autoloaderType + * @throws \League\Flysystem\FilesystemException + */ + protected function addFile(ComposerPackage $dependency, string $packageRelativePath, string $autoloaderType): void { - $sourceAbsoluteFilepath = $dependency->getPackageAbsolutePath() . $packageRelativePath; $outputRelativeFilepath = $dependency->getRelativePath() . $packageRelativePath; $projectRelativePath = $this->vendorDir . $outputRelativeFilepath; diff --git a/src/FileScanner.php b/src/FileScanner.php index 5f0a6e6d..fd6c2b24 100644 --- a/src/FileScanner.php +++ b/src/FileScanner.php @@ -68,11 +68,13 @@ public function findInFiles(DiscoveredFiles $files): DiscoveredSymbols /** * TODO: Don't use preg_replace_callback! * + * @uses self::addDiscoveredNamespaceChange() + * @uses self::addDiscoveredClassChange() + * * @param string $contents */ - protected function find(string $contents, File $file) + protected function find(string $contents, File $file): void { - // If the entire file is under one namespace, all we want is the namespace. // If there were more than one namespace, it would appear as `namespace MyNamespace { ...`, // a file with only a single namespace will appear as `namespace MyNamespace;`. @@ -83,7 +85,7 @@ protected function find(string $contents, File $file) /x'; // # x: ignore whitespace in regex. if (1 === preg_match($singleNamespacePattern, $contents, $matches)) { $this->addDiscoveredNamespaceChange($matches['namespace'], $file); - return $contents; + return; } if (0 < preg_match_all('/\s*define\s*\(\s*["\']([^"\']*)["\']\s*,\s*["\'][^"\']*["\']\s*\)\s*;/', $contents, $constants)) { diff --git a/src/Licenser.php b/src/Licenser.php index e1d153b2..ca524637 100644 --- a/src/Licenser.php +++ b/src/Licenser.php @@ -246,8 +246,7 @@ public function addChangeDeclarationToPhpString( $replaceInMultilineCommentFunction = function ($matches) use ( $licenseDeclaration, - $modifiedDeclaration, - $straussLink + $modifiedDeclaration ) { // Find the line prefix and use it, i.e. could be none, asterisk or space-asterisk. $commentLines = explode("\n", $matches[2]); diff --git a/src/Prefixer.php b/src/Prefixer.php index 8455e92a..f29ea200 100644 --- a/src/Prefixer.php +++ b/src/Prefixer.php @@ -33,7 +33,9 @@ class Prefixer protected array $excludeFilePatternsFromPrefixing; /** - * @var array + * array<$workingDirRelativeFilepath, $package> or null if the file is not from a dependency (i.e. a project file). + * + * @var array */ protected array $changedFiles = array(); @@ -98,17 +100,13 @@ public function replaceInFiles(DiscoveredSymbols $discoveredSymbols, array $phpF } /** - * @param array $namespaceChanges - * @param string[] $classChanges - * @param string[] $constants + * @param DiscoveredSymbols $discoveredSymbols * @param string[] $relativeFilePaths - * - * @throws FileNotFoundException - * @throws Exception + * @return void + * @throws \League\Flysystem\FilesystemException */ public function replaceInProjectFiles(DiscoveredSymbols $discoveredSymbols, array $relativeFilePaths): void { - foreach ($relativeFilePaths as $workingDirRelativeFilepath) { if (! $this->filesystem->fileExists($workingDirRelativeFilepath)) { continue; @@ -120,7 +118,7 @@ public function replaceInProjectFiles(DiscoveredSymbols $discoveredSymbols, arra $updatedContents = $this->replaceInString($discoveredSymbols, $contents); if ($updatedContents !== $contents) { - $this->changedFiles[ $workingDirRelativeFilepath ] = ''; + $this->changedFiles[ $workingDirRelativeFilepath ] = null; $this->filesystem->write($workingDirRelativeFilepath, $updatedContents); } }