Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Apr 25, 2024
1 parent 7a0a927 commit eeecbb7
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/ChangeEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 0 additions & 6 deletions src/Copier.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,9 @@ public function prepareTarget(): void
}
}


/**
*
*/
public function copy(): void
{

/**
* @var string $targetRelativeFilepath
* @var File $file
*/
foreach ($this->files->getFiles() as $file) {
Expand Down
8 changes: 6 additions & 2 deletions src/DependenciesEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class DependenciesEnumerator
protected array $filesAutoloaders = [];

/**
* @param ?array{files?:array<string>, classmap?:array<string>, psr?:array<string,string|array<string>>} $overrideAutoload
* @var array{}|array<string, array{files?:array<string>,classmap?:array<string>,"psr-4":array<string|array<string>>}> $overrideAutoload
*/
protected array $overrideAutoload;
protected array $overrideAutoload = array();

/**
* Constructor.
Expand All @@ -72,6 +72,10 @@ public function __construct(
$this->filesystem = new Filesystem(new LocalFilesystemAdapter($this->workingDir));
}

/**
* @return array<string, ComposerPackage> Packages indexed by package name.
* @throws Exception
*/
public function getAllDependencies(): array
{
$this->recursiveGetAllDependencies($this->requiredPackageNames);
Expand Down
46 changes: 2 additions & 44 deletions src/DiscoveredFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
use ArrayAccess;
use BrianHenryIE\Strauss\Composer\ComposerPackage;

class DiscoveredFiles implements ArrayAccess
class DiscoveredFiles
{

/** @var array<string,File> */
protected array $files = [];

/**
* @param File $file
*/
public function add(File $file)
public function add(File $file): void
{
$this->files[$file->getTargetRelativePath()] = $file;
}
Expand All @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions src/DiscoveredSymbol.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 12 additions & 47 deletions src/DiscoveredSymbols.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,DiscoveredSymbol> */
/**
* All discovered symbols, grouped by type, indexed by original name.
*
* @var array<string,array<string,DiscoveredSymbol>>
*/
protected array $types = [];

public function __construct()
Expand All @@ -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;
}

/**
Expand All @@ -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<string, ConstantSymbol>
*/
Expand All @@ -98,7 +60,10 @@ public function getNamespaces(): array
return $this->types[NamespaceSymbol::class];
}

public function getClasses()
/**
* @return array<string, ClassSymbol>
*/
public function getClasses(): array
{
return $this->types[ClassSymbol::class];
}
Expand Down
8 changes: 4 additions & 4 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/FileEnumerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/FileScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;`.
Expand All @@ -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)) {
Expand Down
3 changes: 1 addition & 2 deletions src/Licenser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
16 changes: 7 additions & 9 deletions src/Prefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class Prefixer
protected array $excludeFilePatternsFromPrefixing;

/**
* @var array<string, ComposerPackage>
* array<$workingDirRelativeFilepath, $package> or null if the file is not from a dependency (i.e. a project file).
*
* @var array<string, ?ComposerPackage>
*/
protected array $changedFiles = array();

Expand Down Expand Up @@ -98,17 +100,13 @@ public function replaceInFiles(DiscoveredSymbols $discoveredSymbols, array $phpF
}

/**
* @param array<string, NamespaceSymbol> $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;
Expand All @@ -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);
}
}
Expand Down

0 comments on commit eeecbb7

Please sign in to comment.