Skip to content

Commit

Permalink
Phpstan level 6
Browse files Browse the repository at this point in the history
  • Loading branch information
convenient committed Nov 7, 2022
1 parent feec2af commit 7c5280d
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 88 deletions.
2 changes: 1 addition & 1 deletion dev/phpstan/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ parameters:
- %currentWorkingDirectory%/stub.php
paths:
- %currentWorkingDirectory%/../../src
level: 5
level: 6
4 changes: 4 additions & 0 deletions src/Ampersand/PatchHelper/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class AnalyseCommand extends Command
public const DOCS_URL
= 'https://github.com/AmpersandHQ/ampersand-magento2-upgrade-patch-helper/blob/master/docs/CHECKS_AVAILABLE.md';

/**
* @inheritDoc
* @return void
*/
protected function configure()
{
$this
Expand Down
50 changes: 28 additions & 22 deletions src/Ampersand/PatchHelper/Helper/Magento2Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,34 @@ class Magento2Instance
/** @var \Magento\Framework\View\Design\FileResolution\Fallback\Resolver\Simple */
private $simpleResolver;

/** @var array */
/** @var string[] */
private $listOfXmlFiles = [];

/** @var array */
/** @var string[] */
private $listOfHtmlFiles = [];

/** @var array */
/** @var array<string, array<int, string>> */
private $dbSchemaThirdPartyAlteration = [];

/** @var array */
/** @var array<string, string> */
private $dbSchemaPrimaryDefinition = [];

/** @var array */
/** @var array<string, array<string, mixed>> */
private $areaConfig = [];

/** @var array */
/** @var array<string, string> */
private $listOfPathsToModules = [];

/** @var array */
/** @var array<string, string> */
private $listOfPathsToLibrarys = [];

/** @var \Throwable[] */
private $bootErrors = [];

public function __construct($path)
/**
* @param string $path
*/
public function __construct(string $path)
{
require rtrim($path, '/') . '/app/bootstrap.php';

Expand Down Expand Up @@ -129,9 +132,10 @@ public function getObjectManager()
/**
* Loads list of all xml files into memory to prevent repeat scans of the file system
*
* @param $directories
* @param string[] $directories
* @return void
*/
private function listXmlFiles($directories)
private function listXmlFiles(array $directories)
{
foreach ($directories as $dir) {
$files = array_filter(explode(PHP_EOL, shell_exec("find {$dir} -name \"*.xml\"")));
Expand All @@ -143,6 +147,7 @@ private function listXmlFiles($directories)

/**
* Prepare the db schema xml data so we have a map of tables to their primary definitions, and alterations
* @return void
*/
private function prepareDbSchemaXmlData()
{
Expand Down Expand Up @@ -231,15 +236,15 @@ private function prepareDbSchemaXmlData()
}

/**
* @return array
* @return array<string, string>
*/
public function getDbSchemaPrimaryDefinition()
{
return $this->dbSchemaPrimaryDefinition;
}

/**
* @return array
* @return array<string, array<int, string>>
*/
public function getDbSchemaThirdPartyAlteration()
{
Expand All @@ -249,9 +254,10 @@ public function getDbSchemaThirdPartyAlteration()
/**
* Loads list of all html files into memory to prevent repeat scans of the file system
*
* @param $directories
* @param string[] $directories
* @return void
*/
private function listHtmlFiles($directories)
private function listHtmlFiles(array $directories)
{
foreach ($directories as $dir) {
$files = array_filter(explode(PHP_EOL, shell_exec("find {$dir} -name \"*.html\"")));
Expand All @@ -262,7 +268,7 @@ private function listHtmlFiles($directories)
}

/**
* @return array
* @return string[]
*/
public function getListOfHtmlFiles()
{
Expand All @@ -278,7 +284,7 @@ public function getBootErrors()
}

/**
* @return array
* @return string[]
*/
public function getListOfXmlFiles()
{
Expand Down Expand Up @@ -325,26 +331,26 @@ public function getConfig()
}

/**
* @return array
* @return array<string, array<string, mixed>>
*/
public function getAreaConfig()
{
return $this->areaConfig;
}

/**
* @return array
* @return string[]
*/
public function getListOfPathsToModules()
{
return $this->listOfPathsToModules;
}

/**
* @param $path
* @return mixed|string
* @param string $path
* @return string
*/
public function getModuleFromPath($path)
public function getModuleFromPath(string $path)
{
$root = rtrim($this->getMagentoRoot(), '/') . '/';
$path = str_replace($root, '', $path);
Expand All @@ -368,7 +374,7 @@ public function getMagentoRoot()
}

/**
* @return array
* @return string[]
*/
public function getListOfPathsToLibrarys()
{
Expand Down
48 changes: 28 additions & 20 deletions src/Ampersand/PatchHelper/Helper/PatchOverrideValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class PatchOverrideValidator
private $m2;

/**
* @var array
* @var array<string, array<string, string>>
*/
private $warnings;

/**
* @var array
* @var array<string, array<string, string>>
*/
private $infos;

Expand All @@ -63,7 +63,7 @@ class PatchOverrideValidator
private $patchEntry;

/**
* @var array
* @var string[]
*/
public static $consumerTypes = [
self::TYPE_QUEUE_CONSUMER_CHANGED,
Expand All @@ -72,7 +72,7 @@ class PatchOverrideValidator
];

/**
* @var array
* @var string[]
*/
public static $dbSchemaTypes = [
self::TYPE_DB_SCHEMA_ADDED,
Expand Down Expand Up @@ -166,12 +166,12 @@ public function canValidate()
}

/**
* @param array $vendorNamespaces
* @param string[] $vendorNamespaces
*
* @return $this
* @throws \Exception
*/
public function validate($vendorNamespaces = [])
public function validate(array $vendorNamespaces = [])
{
switch (pathinfo($this->vendorFilepath, PATHINFO_EXTENSION)) {
case 'php':
Expand Down Expand Up @@ -201,7 +201,7 @@ public function validate($vendorNamespaces = [])
}

/**
* @return array
* @return array<string, array<string, string>>
*/
public function getWarnings()
{
Expand All @@ -217,7 +217,7 @@ public function hasWarnings()
}

/**
* @return array
* @return array<string, array<string, string>>
*/
public function getInfos()
{
Expand All @@ -234,6 +234,8 @@ public function hasInfos()

/**
* Get the warnings in a format for the phpstorm threeway diff
*
* @return array<int, array<int, string>>
*/
public function getThreeWayDiffData()
{
Expand Down Expand Up @@ -263,9 +265,10 @@ public function getThreeWayDiffData()
/**
* Use the object manager to check for preferences
*
* @param array $vendorNamespaces
* @param string[] $vendorNamespaces
* @return void
*/
private function validatePhpFileForPreferences($vendorNamespaces = [])
private function validatePhpFileForPreferences(array $vendorNamespaces = [])
{
$file = $this->appCodeFilepath;

Expand Down Expand Up @@ -301,9 +304,10 @@ private function validatePhpFileForPreferences($vendorNamespaces = [])
/**
* Check for plugins on modified methods within this class
*
* @param array $vendorNamespaces
* @param string[] $vendorNamespaces
* @return void
*/
private function validatePhpFileForPlugins($vendorNamespaces = [])
private function validatePhpFileForPlugins(array $vendorNamespaces = [])
{
$file = $this->appCodeFilepath;

Expand Down Expand Up @@ -410,7 +414,7 @@ class_exists($areaConfig[$area][$pluginClass]['type'])
* Cross reference them with the methods affected in the patch, if there's an intersection the patch
* has updated a public method which has a plugin against it
*/
$intersection = array_intersect_key($methodsIntercepted, $affectedInterceptableMethods);
$intersection = array_filter(array_intersect_key($methodsIntercepted, $affectedInterceptableMethods));

if (!empty($intersection)) {
foreach ($intersection as $methods) {
Expand All @@ -423,10 +427,10 @@ class_exists($areaConfig[$area][$pluginClass]['type'])
}

/**
* @param $class
* @param string $class
* @return false|string
*/
private function getFilenameFromPhpClass($class)
private function getFilenameFromPhpClass(string $class)
{
try {
$refClass = new \ReflectionClass($class);
Expand All @@ -437,13 +441,13 @@ private function getFilenameFromPhpClass($class)
}

/**
* @param $class
* @param $preference
* @param array $vendorNamespaces
* @param string $class
* @param string $preference
* @param string[] $vendorNamespaces
*
* @return bool
*/
private function isThirdPartyPreference($class, $preference, $vendorNamespaces = [])
private function isThirdPartyPreference(string $class, string $preference, array $vendorNamespaces = [])
{
if ($preference === $class || $preference === "$class\\Interceptor") {
// Class is not overridden
Expand Down Expand Up @@ -488,6 +492,7 @@ private function isThirdPartyPreference($class, $preference, $vendorNamespaces =
/**
* @param string $type
* @throws \Exception
* @return void
*/
private function validateFrontendFile($type)
{
Expand Down Expand Up @@ -544,6 +549,7 @@ private function validateFrontendFile($type)

/**
* Knockout html files live in web directory
* @return void
*/
private function validateWebTemplateHtml()
{
Expand Down Expand Up @@ -586,6 +592,7 @@ function ($potentialFilePath) use ($module, $templatePart) {

/**
* Email templates live in theme directory like `theme/Magento_Customer/email/foobar.html
* @return void
*/
private function validateEmailTemplateHtml()
{
Expand Down Expand Up @@ -688,7 +695,7 @@ private function validateDbSchemaFile()
unset($tableName, $definition);

foreach ($newDefinitions as $tableName => $newDefinition) {
if (!(isset($originalDefinitions[$tableName]) && isset($newDefinitions[$tableName]))) {
if (!(isset($originalDefinitions[$tableName]) && is_array($newDefinition))) {
continue; // This table is not defined in the original and new definitions
}
if ($originalDefinitions[$tableName]['amp_upgrade_hash'] === $newDefinition['amp_upgrade_hash']) {
Expand Down Expand Up @@ -767,6 +774,7 @@ private function validateDbSchemaFile()

/**
* Search the app and vendor directory for layout files with the same name, for the same module.
* @return void
*/
private function validateLayoutFile()
{
Expand Down
Loading

0 comments on commit 7c5280d

Please sign in to comment.