Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
- Remove unused `use` statements
- Change FQN to use statements
- Make inline functions static
- Add type hint for parameters
- Add return types
- Remove redundant docblocks and inspection comments
- Add @throws annotations
  • Loading branch information
Potherca committed May 5, 2024
1 parent a24e519 commit 591f4b6
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 222 deletions.
7 changes: 4 additions & 3 deletions bin/php-scanner
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace Potherca\Scanner;

use Potherca\Scanner\CommandLineInterface;
use Potherca\Scanner\CommandLineInterface\Arguments;
use Potherca\Scanner\CommandLineInterface\Command;
use Potherca\Scanner\Identifier\IdentifierOption;
use Potherca\Scanner\Identifier\InternalFunctionsIdentifier;
use Symfony\Component\Finder\Finder;
Expand All @@ -22,13 +23,13 @@ $defaultArguments = [

/*/ Create Objects /*/
$finder = new Finder();
$command = new CommandLineInterface\Command(basename(__FILE__), $argv);
$command = new Command(basename(__FILE__), $argv);

/*/ Build arguments /*/
$optArguments = getopt($command->getShortOptions(), $command->getLongOptions());
$optArguments = array_merge($defaultArguments, $optArguments);

$arguments = new CommandLineInterface\Arguments($optArguments, $finder);
$arguments = new Arguments($optArguments, $finder);

$output = $command->call($arguments);

Expand Down
15 changes: 5 additions & 10 deletions src/ArgumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@

interface ArgumentInterface
{
/** @return array */
public function getBlacklist();
public function getBlacklist(): array;

/** @return array */
public function getDirectories();
public function getDirectories(): array;

/** @return array */
public function getWhitelist();
public function getWhitelist(): array;

/** @return int */
public function getPhpVersion();
public function getPhpVersion(): int;

/** @return array */
public function getIdentifiers();
public function getIdentifiers(): array;
}

/*EOF*/
93 changes: 36 additions & 57 deletions src/CommandLineInterface/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,56 +37,47 @@ class Arguments implements ArgumentInterface
private $whitelist = [];

//////////////////////////// SETTERS AND GETTERS \\\\\\\\\\\\\\\\\\\\\\\\\\\
/** @return array */
public function getBlacklist()
public function getBlacklist(): array
{
return $this->blacklist;
}

/** @return array */
public function getDirectories()
public function getDirectories(): array
{
return $this->directories;
}

/** @return int */
public function getErrorCode()
public function getErrorCode(): int
{
return $this->errorCode;
}

/** @return string */
final public function getErrorMessage()
final public function getErrorMessage(): string
{
return $this->errorMessage;
}

/** @return array */
public function getIdentifiers()
public function getIdentifiers(): array
{
return $this->identifiers;
}

/** @return int */
public function getPhpVersion()
public function getPhpVersion(): int
{
return $this->phpVersion;
}

/** @return array */
public function getWhitelist()
public function getWhitelist(): array
{
return $this->whitelist;
}

/** @return bool */
public function isHelp()
public function isHelp(): bool
{
return $this->isHelp;
}

/** @return bool */
public function isVerbose()
public function isVerbose(): bool
{
return $this->isVerbose;
}
Expand All @@ -102,7 +93,7 @@ final public function parse()
{
$arguments = $this->arguments;
// @TODO: Use Symfony Finder instead of hard-coded IO lookup
$finder = $this->finder;
// $finder = $this->finder;

$this->isVerbose = array_key_exists('verbose', $arguments);

Expand Down Expand Up @@ -130,7 +121,7 @@ final public function parse()
}
}

final public function isValid()
final public function isValid(): bool
{
return $this->errorCode === 0;
}
Expand All @@ -142,48 +133,43 @@ final public function loadIdentifiers()
}

////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
/**
* @param string $key
*/
private function loadSpecificIdentifiers($key)
private function loadSpecificIdentifiers(string $key)
{
$arguments = $this->arguments;

if ($this->isValid() === true) {
if (array_key_exists($key, $arguments)) {
$identifiers = $arguments[$key];
if (
$this->isValid() === true
&& array_key_exists($key, $arguments)
) {
$identifiers = $arguments[$key];

if (is_scalar($identifiers)) {
$identifiers = [$identifiers];
}
if (is_scalar($identifiers)) {
$identifiers = [$identifiers];
}

$identifierPaths = [];
$identifierPaths = [];

array_walk($identifiers, function ($identifier) use (&$identifierPaths) {
/* @NOTE: Remove any trailing slash */
$identifier = rtrim($identifier, '/');
array_walk($identifiers, function ($identifier) use (&$identifierPaths) {
/* @NOTE: Remove any trailing slash */
$identifier = rtrim($identifier, '/');

if (is_dir($identifier)) {
$files = glob($identifier . '/*.php');
if (is_dir($identifier)) {
$files = glob($identifier . '/*.php');

$identifierPaths = array_merge($identifierPaths, $files);
} elseif (is_file($identifier)) {
$identifierPaths[] = $identifier;
} else {
$this->errorMessage = sprintf('Given identifier "%s" is not a file or directory', $identifier);
$this->errorCode = self::ERROR_SUBJECT_NOT_FILE_OR_FOLDER;
}
});
$identifierPaths = array_merge($identifierPaths, $files);
} elseif (is_file($identifier)) {
$identifierPaths[] = $identifier;
} else {
$this->errorMessage = sprintf('Given identifier "%s" is not a file or directory', $identifier);
$this->errorCode = self::ERROR_SUBJECT_NOT_FILE_OR_FOLDER;
}
});

$identifierPaths = array_filter($identifierPaths);
$this->identifiers = array_merge($this->identifiers, $identifierPaths);
}
$identifierPaths = array_filter($identifierPaths);
$this->identifiers = array_merge($this->identifiers, $identifierPaths);
}
}

/**
* @param $arguments
*/
private function loadDirectories($arguments)
{
if ($this->isValid() === true && array_key_exists('subject', $arguments)) {
Expand All @@ -194,7 +180,6 @@ private function loadDirectories($arguments)
$subjects = [$subjects];
}

/** @noinspection ForeachSourceInspection */
foreach ($subjects as $subject) {
if (is_dir($subject)) {
$this->directories[] = $subject;
Expand All @@ -209,9 +194,6 @@ private function loadDirectories($arguments)
}
}

/**
* @param $arguments
*/
private function loadBlackList($arguments)
{
if ($this->isValid() === true) {
Expand All @@ -228,9 +210,6 @@ private function loadBlackList($arguments)
}
}

/**
* @param $arguments
*/
private function loadPhpVersion($arguments)
{
$key = IdentifierOption::PHP_VERSION;
Expand Down
Loading

0 comments on commit 591f4b6

Please sign in to comment.