From 7c5280deedd712137b317f6f4ef9f937d457b941 Mon Sep 17 00:00:00 2001 From: Luke Rodgers Date: Mon, 7 Nov 2022 11:55:04 +0000 Subject: [PATCH] Phpstan level 6 --- dev/phpstan/phpstan.neon | 2 +- .../PatchHelper/Command/AnalyseCommand.php | 4 + .../PatchHelper/Helper/Magento2Instance.php | 50 +++++++------ .../Helper/PatchOverrideValidator.php | 48 +++++++----- src/Ampersand/PatchHelper/Patchfile/Entry.php | 73 +++++++++++-------- .../PatchHelper/Patchfile/Reader.php | 2 + src/functions.php | 30 ++++---- 7 files changed, 121 insertions(+), 88 deletions(-) diff --git a/dev/phpstan/phpstan.neon b/dev/phpstan/phpstan.neon index 65bfe1e5..335833d7 100644 --- a/dev/phpstan/phpstan.neon +++ b/dev/phpstan/phpstan.neon @@ -3,4 +3,4 @@ parameters: - %currentWorkingDirectory%/stub.php paths: - %currentWorkingDirectory%/../../src - level: 5 \ No newline at end of file + level: 6 \ No newline at end of file diff --git a/src/Ampersand/PatchHelper/Command/AnalyseCommand.php b/src/Ampersand/PatchHelper/Command/AnalyseCommand.php index 996c16a6..8cc8e7e2 100644 --- a/src/Ampersand/PatchHelper/Command/AnalyseCommand.php +++ b/src/Ampersand/PatchHelper/Command/AnalyseCommand.php @@ -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 diff --git a/src/Ampersand/PatchHelper/Helper/Magento2Instance.php b/src/Ampersand/PatchHelper/Helper/Magento2Instance.php index fed11e82..fc2017da 100644 --- a/src/Ampersand/PatchHelper/Helper/Magento2Instance.php +++ b/src/Ampersand/PatchHelper/Helper/Magento2Instance.php @@ -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> */ private $dbSchemaThirdPartyAlteration = []; - /** @var array */ + /** @var array */ private $dbSchemaPrimaryDefinition = []; - /** @var array */ + /** @var array> */ private $areaConfig = []; - /** @var array */ + /** @var array */ private $listOfPathsToModules = []; - /** @var array */ + /** @var array */ private $listOfPathsToLibrarys = []; /** @var \Throwable[] */ private $bootErrors = []; - public function __construct($path) + /** + * @param string $path + */ + public function __construct(string $path) { require rtrim($path, '/') . '/app/bootstrap.php'; @@ -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\""))); @@ -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() { @@ -231,7 +236,7 @@ private function prepareDbSchemaXmlData() } /** - * @return array + * @return array */ public function getDbSchemaPrimaryDefinition() { @@ -239,7 +244,7 @@ public function getDbSchemaPrimaryDefinition() } /** - * @return array + * @return array> */ public function getDbSchemaThirdPartyAlteration() { @@ -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\""))); @@ -262,7 +268,7 @@ private function listHtmlFiles($directories) } /** - * @return array + * @return string[] */ public function getListOfHtmlFiles() { @@ -278,7 +284,7 @@ public function getBootErrors() } /** - * @return array + * @return string[] */ public function getListOfXmlFiles() { @@ -325,7 +331,7 @@ public function getConfig() } /** - * @return array + * @return array> */ public function getAreaConfig() { @@ -333,7 +339,7 @@ public function getAreaConfig() } /** - * @return array + * @return string[] */ public function getListOfPathsToModules() { @@ -341,10 +347,10 @@ public function getListOfPathsToModules() } /** - * @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); @@ -368,7 +374,7 @@ public function getMagentoRoot() } /** - * @return array + * @return string[] */ public function getListOfPathsToLibrarys() { diff --git a/src/Ampersand/PatchHelper/Helper/PatchOverrideValidator.php b/src/Ampersand/PatchHelper/Helper/PatchOverrideValidator.php index d7064390..a612d100 100644 --- a/src/Ampersand/PatchHelper/Helper/PatchOverrideValidator.php +++ b/src/Ampersand/PatchHelper/Helper/PatchOverrideValidator.php @@ -48,12 +48,12 @@ class PatchOverrideValidator private $m2; /** - * @var array + * @var array> */ private $warnings; /** - * @var array + * @var array> */ private $infos; @@ -63,7 +63,7 @@ class PatchOverrideValidator private $patchEntry; /** - * @var array + * @var string[] */ public static $consumerTypes = [ self::TYPE_QUEUE_CONSUMER_CHANGED, @@ -72,7 +72,7 @@ class PatchOverrideValidator ]; /** - * @var array + * @var string[] */ public static $dbSchemaTypes = [ self::TYPE_DB_SCHEMA_ADDED, @@ -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': @@ -201,7 +201,7 @@ public function validate($vendorNamespaces = []) } /** - * @return array + * @return array> */ public function getWarnings() { @@ -217,7 +217,7 @@ public function hasWarnings() } /** - * @return array + * @return array> */ public function getInfos() { @@ -234,6 +234,8 @@ public function hasInfos() /** * Get the warnings in a format for the phpstorm threeway diff + * + * @return array> */ public function getThreeWayDiffData() { @@ -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; @@ -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; @@ -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) { @@ -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); @@ -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 @@ -488,6 +492,7 @@ private function isThirdPartyPreference($class, $preference, $vendorNamespaces = /** * @param string $type * @throws \Exception + * @return void */ private function validateFrontendFile($type) { @@ -544,6 +549,7 @@ private function validateFrontendFile($type) /** * Knockout html files live in web directory + * @return void */ private function validateWebTemplateHtml() { @@ -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() { @@ -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']) { @@ -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() { diff --git a/src/Ampersand/PatchHelper/Patchfile/Entry.php b/src/Ampersand/PatchHelper/Patchfile/Entry.php index 106a7f65..edae5698 100644 --- a/src/Ampersand/PatchHelper/Patchfile/Entry.php +++ b/src/Ampersand/PatchHelper/Patchfile/Entry.php @@ -27,11 +27,11 @@ class Entry /** * Entry constructor. - * @param $directory - * @param $newFilepath - * @param $originalFilepath + * @param string $directory + * @param string $newFilepath + * @param string $originalFilepath */ - public function __construct($directory, $newFilepath, $originalFilepath) + public function __construct(string $directory, string $newFilepath, string $originalFilepath) { $this->directory = $directory; $this->newFilePath = $newFilepath; @@ -55,9 +55,10 @@ public function getOriginalPath() } /** - * @param $string + * @param string $string + * @return void */ - public function addLine($string) + public function addLine(string $string) { $this->lines[] = $string; } @@ -88,7 +89,7 @@ public function fileWasAdded() /** * Read the patch file and split into affected chunks * - * @return array + * @return array> */ public function getHunks() { @@ -132,10 +133,10 @@ public function getHunks() /** * Gather the line numbers (and content) removed from the original file, and added to the new file - * @param $hunks - * @return array + * @param array> $hunks + * @return array> */ - public function getModifiedLines($hunks) + public function getModifiedLines(array $hunks) { $modifiedLines = [ 'new' => [], @@ -183,7 +184,8 @@ public function getModifiedLines($hunks) } /** - * return string[] + * @return string[] + * @throws PluginDetectionException */ public function getAffectedInterceptablePhpFunctions() { @@ -246,13 +248,13 @@ public function getAffectedInterceptablePhpFunctions() } /** - * @param $lineNumber - * @param $fileContents - * @param $expectedLineContents + * @param int $lineNumber + * @param string[] $fileContents + * @param string $expectedLineContents * @return bool|string * @throws PluginDetectionException */ - private function getAffectedFunction($lineNumber, $fileContents, $expectedLineContents) + private function getAffectedFunction(int $lineNumber, array $fileContents, string $expectedLineContents) { // minus one for the array index starting at zero $actualLine = $fileContents[$lineNumber - 1]; @@ -267,11 +269,11 @@ private function getAffectedFunction($lineNumber, $fileContents, $expectedLineCo } /** - * @param $fileContents - * @param $lineNumber + * @param string[] $fileContents + * @param int $lineNumber * @return bool|string */ - private function scanAboveForFunctionDeclaration($fileContents, $lineNumber) + private function scanAboveForFunctionDeclaration(array $fileContents, int $lineNumber) { $line = trim($fileContents[$lineNumber]); @@ -308,10 +310,10 @@ private function scanAboveForFunctionDeclaration($fileContents, $lineNumber) } /** - * @param $path - * @return array + * @param string $path + * @return string[] */ - private function getFileContents($path) + private function getFileContents(string $path) { $filepath = realpath($this->directory . DIRECTORY_SEPARATOR . $path); if (!is_file($filepath)) { @@ -329,11 +331,17 @@ public function __toString() return implode(PHP_EOL, $this->lines); } - public function applyToTheme($projectDir, $overrideFile, $fuzzFactor) + /** + * @param string $projectDir + * @param string $overrideFile + * @param int $fuzzFactor + * @return void + */ + public function applyToTheme(string $projectDir, string $overrideFile, int $fuzzFactor) { $overrideFilePathRelative = sanitize_filepath($projectDir, $overrideFile); - if (substr($overrideFilePathRelative, 0, 7) === "vendor/") { + if (str_starts_with($overrideFilePathRelative, 'vendor/')) { return; // Only attempt to patch local files not vendor overrides which will be in .gitignore } @@ -355,9 +363,10 @@ public function applyToTheme($projectDir, $overrideFile, $fuzzFactor) /** * Get Added/Removed Queue Consumers * - * @return array + * @param string $modifiedLineType + * @return string[] */ - private function getAddedOrRemovedQueueConsumers($modifiedLineType = 'new') + private function getAddedOrRemovedQueueConsumers(string $modifiedLineType = 'new') { if (pathinfo($this->newFilePath, PATHINFO_BASENAME) !== 'queue_consumer.xml') { // try to get added consumers on a wrong filename @@ -383,7 +392,7 @@ private function getAddedOrRemovedQueueConsumers($modifiedLineType = 'new') /** * Get Added Queue Consumers * - * @return array + * @return string[] */ public function getAddedQueueConsumers() { @@ -393,7 +402,7 @@ public function getAddedQueueConsumers() /** * Get Removed Queue Consumers * - * @return array + * @return string[] */ public function getRemovedQueueConsumers() { @@ -401,7 +410,7 @@ public function getRemovedQueueConsumers() } /** - * @return array + * @return array> */ public function getDatabaseTablesDefinitionsFromOriginalFile() { @@ -409,7 +418,7 @@ public function getDatabaseTablesDefinitionsFromOriginalFile() } /** - * @return array + * @return array> */ public function getDatabaseTablesDefinitionsFromNewFile() { @@ -417,10 +426,10 @@ public function getDatabaseTablesDefinitionsFromNewFile() } /** - * @param $file - * @return array + * @param string $file + * @return array> */ - private function getDatabaseTablesDefinitionsFromFile($file) + private function getDatabaseTablesDefinitionsFromFile(string $file) { if (pathinfo($file, PATHINFO_BASENAME) !== 'db_schema.xml') { return []; // try to get database schema info from wrong file diff --git a/src/Ampersand/PatchHelper/Patchfile/Reader.php b/src/Ampersand/PatchHelper/Patchfile/Reader.php index 168c4b3f..157d724d 100644 --- a/src/Ampersand/PatchHelper/Patchfile/Reader.php +++ b/src/Ampersand/PatchHelper/Patchfile/Reader.php @@ -61,6 +61,8 @@ public function getFiles() /** * Resets the file, should be called after modifying the file + * + * @return void */ private function reset() { diff --git a/src/functions.php b/src/functions.php index b0164b6f..cd8155e9 100644 --- a/src/functions.php +++ b/src/functions.php @@ -12,11 +12,11 @@ /** * Returns true only if $string contains $contains * - * @param $string - * @param $contains + * @param string $string + * @param string $contains * @return bool */ - function str_contains($string, $contains) + function str_contains(string $string, string $contains) { return strpos($string, $contains) !== false; } @@ -26,11 +26,11 @@ function str_contains($string, $contains) /** * Returns true only if $string starts with $startsWith * - * @param $string - * @param $startsWith + * @param string $string + * @param string $startsWith * @return bool */ - function str_starts_with($string, $startsWith) + function str_starts_with(string $string, string $startsWith) { return substr($string, 0, strlen($startsWith)) === $startsWith; } @@ -40,11 +40,11 @@ function str_starts_with($string, $startsWith) /** * Returns true only if $string ends with $endsWith * - * @param $string - * @param $endsWith + * @param string $string + * @param string $endsWith * @return bool */ - function str_ends_with($string, $endsWith) + function str_ends_with(string $string, string $endsWith) { return strlen($endsWith) == 0 || substr($string, -strlen($endsWith)) === $endsWith; } @@ -52,7 +52,11 @@ function str_ends_with($string, $endsWith) if (!function_exists('recur_ksort')) { // https://stackoverflow.com/a/4501406/4354325 - function recur_ksort(&$array) + /** + * @param array $array + * @return bool + */ + function recur_ksort(array &$array) { foreach ($array as &$value) { if (is_array($value)) { @@ -66,11 +70,11 @@ function recur_ksort(&$array) /** * Strip the project directory from the filepath * - * @param $projectDir - * @param $filepath + * @param string $projectDir + * @param string $filepath * @return string */ -function sanitize_filepath($projectDir, $filepath) +function sanitize_filepath(string $projectDir, string $filepath) { return ltrim(str_replace(realpath($projectDir), '', $filepath), '/'); }