From 870cb8c1ebac89d9056e2a2317f20b1733452ac6 Mon Sep 17 00:00:00 2001 From: Ivan Makarenkov Date: Thu, 20 Jan 2022 09:14:33 +0300 Subject: [PATCH 1/3] Add method return type changes information --- .../Analyzer/ClassAnalyzer.php | 2 +- .../Analyzer/ClassMethodAnalyzer.php | 26 +++++++++++++++++++ .../Analyzer/InterfaceAnalyzer.php | 2 +- .../Analyzer/TraitAnalyzer.php | 2 +- .../Configuration/LevelMapping.php | 6 +++++ .../Operation/ClassMethodReturnTypeAdded.php | 19 ++++++++++++++ .../ClassMethodReturnTypeChanged.php | 19 ++++++++++++++ .../ClassMethodReturnTypeRemoved.php | 19 ++++++++++++++ 8 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 src/PHPSemVerChecker/Operation/ClassMethodReturnTypeAdded.php create mode 100644 src/PHPSemVerChecker/Operation/ClassMethodReturnTypeChanged.php create mode 100644 src/PHPSemVerChecker/Operation/ClassMethodReturnTypeRemoved.php diff --git a/src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php b/src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php index e21d0c0..7c5a0fe 100644 --- a/src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/ClassAnalyzer.php @@ -67,7 +67,7 @@ public function analyze(Registry $registryBefore, Registry $registryAfter) if ($classBefore != $classAfter) { // Check for case change of class name. // If we entered this section then the normalized names (lowercase) were equal. - if ($classBefore->name !== $classAfter->name) { + if ($classBefore->name->toString() !== $classAfter->name->toString()) { $report->add( $this->context, new ClassCaseChanged( diff --git a/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php b/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php index 86a766d..1220b9d 100644 --- a/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php @@ -18,6 +18,9 @@ use PHPSemVerChecker\Operation\ClassMethodParameterTypingAdded; use PHPSemVerChecker\Operation\ClassMethodParameterTypingRemoved; use PHPSemVerChecker\Operation\ClassMethodRemoved; +use PHPSemVerChecker\Operation\ClassMethodReturnTypeAdded; +use PHPSemVerChecker\Operation\ClassMethodReturnTypeChanged; +use PHPSemVerChecker\Operation\ClassMethodReturnTypeRemoved; use PHPSemVerChecker\Report\Report; class ClassMethodAnalyzer @@ -109,6 +112,29 @@ public function analyze(Stmt $contextBefore, Stmt $contextAfter) ); } + if ($methodBefore->returnType !== $methodAfter->returnType) { + $class = ClassMethodCaseChanged::class; + if ($methodBefore->returnType === null) { + $class = ClassMethodReturnTypeAdded::class; + } elseif ($methodAfter->returnType === null) { + $class = ClassMethodReturnTypeRemoved::class; + } elseif ($methodAfter->returnType->name !== $methodBefore->returnType->name) { + $class = ClassMethodReturnTypeChanged::class; + } + $report->add( + $this->context, + new $class( + $this->context, + $this->fileBefore, + $contextAfter, + $methodBefore, + $this->fileAfter, + $contextAfter, + $methodAfter + ) + ); + } + $signatureResult = Signature::analyze($methodBefore->getParams(), $methodAfter->getParams()); $changes = [ diff --git a/src/PHPSemVerChecker/Analyzer/InterfaceAnalyzer.php b/src/PHPSemVerChecker/Analyzer/InterfaceAnalyzer.php index a83248b..e7e68c2 100644 --- a/src/PHPSemVerChecker/Analyzer/InterfaceAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/InterfaceAnalyzer.php @@ -67,7 +67,7 @@ public function analyze(Registry $registryBefore, Registry $registryAfter) if ($interfaceBefore != $interfaceAfter) { // Check if the name of the interface has changed case. // If we entered this section then the normalized names (lowercase) were equal. - if ($interfaceBefore->name !== $interfaceAfter->name) { + if ($interfaceBefore->name->toString() !== $interfaceAfter->name->toString()) { $report->add( 'interface', new InterfaceCaseChanged( diff --git a/src/PHPSemVerChecker/Analyzer/TraitAnalyzer.php b/src/PHPSemVerChecker/Analyzer/TraitAnalyzer.php index b81e53d..6f54a31 100644 --- a/src/PHPSemVerChecker/Analyzer/TraitAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/TraitAnalyzer.php @@ -65,7 +65,7 @@ public function analyze(Registry $registryBefore, Registry $registryAfter) if ($traitBefore != $traitAfter) { // Check for name case change. // If we entered this section then the normalized names (lowercase) were equal. - if ($traitBefore->name !== $traitAfter->name) { + if ($traitBefore->name->toString() !== $traitAfter->name->toString()) { $report->add( $this->context, new TraitCaseChanged( diff --git a/src/PHPSemVerChecker/Configuration/LevelMapping.php b/src/PHPSemVerChecker/Configuration/LevelMapping.php index 5bd98ad..af6ff92 100644 --- a/src/PHPSemVerChecker/Configuration/LevelMapping.php +++ b/src/PHPSemVerChecker/Configuration/LevelMapping.php @@ -135,6 +135,12 @@ class LevelMapping 'V158' => Level::PATCH, 'V159' => Level::PATCH, 'V160' => Level::PATCH, + 'V161' => Level::PATCH, + 'V162' => Level::PATCH, + 'V163' => Level::PATCH, + 'V164' => Level::PATCH, + 'V165' => Level::PATCH, + 'V166' => Level::PATCH, ]; /** diff --git a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeAdded.php b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeAdded.php new file mode 100644 index 0000000..4a4f626 --- /dev/null +++ b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeAdded.php @@ -0,0 +1,19 @@ + ['V161'], + 'interface' => ['V162'], + 'trait' => ['V163'], + ]; + /** + * @var string + */ + protected $reason = 'Method return type added.'; +} diff --git a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeChanged.php b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeChanged.php new file mode 100644 index 0000000..0831213 --- /dev/null +++ b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeChanged.php @@ -0,0 +1,19 @@ + ['V164'], + 'interface' => ['V165'], + 'trait' => ['V166'], + ]; + /** + * @var string + */ + protected $reason = 'Method return type changed.'; +} diff --git a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeRemoved.php b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeRemoved.php new file mode 100644 index 0000000..a6db141 --- /dev/null +++ b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeRemoved.php @@ -0,0 +1,19 @@ + ['V164'], + 'interface' => ['V165'], + 'trait' => ['V166'], + ]; + /** + * @var string + */ + protected $reason = 'Method return type removed.'; +} From 25f03ea7376cc3b75e8983ebd838a7bbed6883db Mon Sep 17 00:00:00 2001 From: Ivan Makarenkov Date: Fri, 21 Jan 2022 10:52:28 +0300 Subject: [PATCH 2/3] Review fixes --- .../Analyzer/ClassMethodAnalyzer.php | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php b/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php index 1220b9d..d96d8a3 100644 --- a/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php @@ -113,26 +113,28 @@ public function analyze(Stmt $contextBefore, Stmt $contextAfter) } if ($methodBefore->returnType !== $methodAfter->returnType) { - $class = ClassMethodCaseChanged::class; - if ($methodBefore->returnType === null) { + $class = null; + if ($methodBefore->returnType !== null && $methodAfter->returnType === null) { $class = ClassMethodReturnTypeAdded::class; - } elseif ($methodAfter->returnType === null) { + } elseif ($methodAfter->returnType === null && $methodBefore->returnType === null) { $class = ClassMethodReturnTypeRemoved::class; - } elseif ($methodAfter->returnType->name !== $methodBefore->returnType->name) { + } elseif (strcasecmp($methodAfter->returnType->name, $methodBefore->returnType->name) !== 0) { $class = ClassMethodReturnTypeChanged::class; } - $report->add( - $this->context, - new $class( + if ($class) { + $report->add( $this->context, - $this->fileBefore, - $contextAfter, - $methodBefore, - $this->fileAfter, - $contextAfter, - $methodAfter - ) - ); + new $class( + $this->context, + $this->fileBefore, + $contextAfter, + $methodBefore, + $this->fileAfter, + $contextAfter, + $methodAfter + ) + ); + } } $signatureResult = Signature::analyze($methodBefore->getParams(), $methodAfter->getParams()); From 938beaf12f7e315c3495d9dc25fde559f44fadf4 Mon Sep 17 00:00:00 2001 From: Ivan Makarenkov Date: Wed, 26 Jan 2022 18:54:31 +0300 Subject: [PATCH 3/3] Functions return type fix --- .../Analyzer/ClassMethodAnalyzer.php | 2 +- .../Analyzer/FunctionAnalyzer.php | 27 +++++++++++++++++++ .../Configuration/LevelMapping.php | 6 +++++ .../Operation/ClassMethodReturnTypeAdded.php | 2 +- .../ClassMethodReturnTypeChanged.php | 8 +++--- .../ClassMethodReturnTypeRemoved.php | 2 +- .../Operation/FunctionReturnTypeAdded.php | 15 +++++++++++ .../Operation/FunctionReturnTypeChanged.php | 15 +++++++++++ .../Operation/FunctionReturnTypeRemoved.php | 15 +++++++++++ 9 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 src/PHPSemVerChecker/Operation/FunctionReturnTypeAdded.php create mode 100644 src/PHPSemVerChecker/Operation/FunctionReturnTypeChanged.php create mode 100644 src/PHPSemVerChecker/Operation/FunctionReturnTypeRemoved.php diff --git a/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php b/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php index d96d8a3..f1f8973 100644 --- a/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/ClassMethodAnalyzer.php @@ -116,7 +116,7 @@ public function analyze(Stmt $contextBefore, Stmt $contextAfter) $class = null; if ($methodBefore->returnType !== null && $methodAfter->returnType === null) { $class = ClassMethodReturnTypeAdded::class; - } elseif ($methodAfter->returnType === null && $methodBefore->returnType === null) { + } elseif ($methodAfter->returnType !== null && $methodBefore->returnType === null) { $class = ClassMethodReturnTypeRemoved::class; } elseif (strcasecmp($methodAfter->returnType->name, $methodBefore->returnType->name) !== 0) { $class = ClassMethodReturnTypeChanged::class; diff --git a/src/PHPSemVerChecker/Analyzer/FunctionAnalyzer.php b/src/PHPSemVerChecker/Analyzer/FunctionAnalyzer.php index 3d8fc60..0712dbb 100644 --- a/src/PHPSemVerChecker/Analyzer/FunctionAnalyzer.php +++ b/src/PHPSemVerChecker/Analyzer/FunctionAnalyzer.php @@ -18,6 +18,9 @@ use PHPSemVerChecker\Operation\FunctionParameterTypingAdded; use PHPSemVerChecker\Operation\FunctionParameterTypingRemoved; use PHPSemVerChecker\Operation\FunctionRemoved; +use PHPSemVerChecker\Operation\FunctionReturnTypeAdded; +use PHPSemVerChecker\Operation\FunctionReturnTypeChanged; +use PHPSemVerChecker\Operation\FunctionReturnTypeRemoved; use PHPSemVerChecker\Registry\Registry; use PHPSemVerChecker\Report\Report; @@ -89,6 +92,30 @@ public function analyze(Registry $registryBefore, Registry $registryAfter) ); } + if ($functionBefore->returnType !== $functionAfter->returnType) { + print_r($functionBefore->returnType); + print_r($functionAfter->returnType); + $class = null; + if ($functionBefore->returnType !== null && $functionAfter->returnType === null) { + $class = FunctionReturnTypeAdded::class; + } elseif ($functionAfter->returnType !== null && $functionBefore->returnType === null) { + $class = FunctionReturnTypeRemoved::class; + } elseif (strcasecmp($functionAfter->returnType->name, $functionBefore->returnType->name) !== 0) { + $class = FunctionReturnTypeChanged::class; + } + if ($class) { + $report->add( + $this->context, + new $class( + $fileBefore, + $functionBefore, + $fileAfter, + $functionAfter + ) + ); + } + } + $signatureResult = Signature::analyze($functionBefore->getParams(), $functionAfter->getParams()); $changes = [ diff --git a/src/PHPSemVerChecker/Configuration/LevelMapping.php b/src/PHPSemVerChecker/Configuration/LevelMapping.php index af6ff92..b8572f1 100644 --- a/src/PHPSemVerChecker/Configuration/LevelMapping.php +++ b/src/PHPSemVerChecker/Configuration/LevelMapping.php @@ -141,6 +141,12 @@ class LevelMapping 'V164' => Level::PATCH, 'V165' => Level::PATCH, 'V166' => Level::PATCH, + 'V167' => Level::PATCH, + 'V168' => Level::PATCH, + 'V169' => Level::PATCH, + 'V170' => Level::PATCH, + 'V171' => Level::PATCH, + 'V172' => Level::PATCH, ]; /** diff --git a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeAdded.php b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeAdded.php index 4a4f626..e4b45de 100644 --- a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeAdded.php +++ b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeAdded.php @@ -15,5 +15,5 @@ class ClassMethodReturnTypeAdded extends ClassMethodOperationDelta /** * @var string */ - protected $reason = 'Method return type added.'; + protected $reason = 'Method return type was added.'; } diff --git a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeChanged.php b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeChanged.php index 0831213..57d1800 100644 --- a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeChanged.php +++ b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeChanged.php @@ -8,12 +8,12 @@ class ClassMethodReturnTypeChanged extends ClassMethodOperationDelta * @var array */ protected $code = [ - 'class' => ['V164'], - 'interface' => ['V165'], - 'trait' => ['V166'], + 'class' => ['V167'], + 'interface' => ['V168'], + 'trait' => ['V169'], ]; /** * @var string */ - protected $reason = 'Method return type changed.'; + protected $reason = 'Method return type was changed.'; } diff --git a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeRemoved.php b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeRemoved.php index a6db141..e321cdc 100644 --- a/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeRemoved.php +++ b/src/PHPSemVerChecker/Operation/ClassMethodReturnTypeRemoved.php @@ -15,5 +15,5 @@ class ClassMethodReturnTypeRemoved extends ClassMethodOperationDelta /** * @var string */ - protected $reason = 'Method return type removed.'; + protected $reason = 'Method return type was removed.'; } diff --git a/src/PHPSemVerChecker/Operation/FunctionReturnTypeAdded.php b/src/PHPSemVerChecker/Operation/FunctionReturnTypeAdded.php new file mode 100644 index 0000000..767555e --- /dev/null +++ b/src/PHPSemVerChecker/Operation/FunctionReturnTypeAdded.php @@ -0,0 +1,15 @@ +