|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPSemVerChecker\Analyzer; |
| 4 | + |
| 5 | +use PhpParser\Node\Stmt\Class_; |
| 6 | +use PhpParser\Node\Stmt\ClassMethod; |
| 7 | +use PHPSemVerChecker\Comparator\Signature; |
| 8 | +use PHPSemVerChecker\Operation\ClassMethodAdded; |
| 9 | +use PHPSemVerChecker\Operation\ClassMethodImplementationChanged; |
| 10 | +use PHPSemVerChecker\Operation\ClassMethodParameterChanged; |
| 11 | +use PHPSemVerChecker\Operation\ClassMethodRemoved; |
| 12 | +use PHPSemVerChecker\Operation\Unknown; |
| 13 | +use PHPSemVerChecker\Report\Report; |
| 14 | +use PHPSemVerChecker\SemanticVersioning\Level; |
| 15 | + |
| 16 | +class ClassMethodAnalyzer |
| 17 | +{ |
| 18 | + protected $context = 'method'; |
| 19 | + |
| 20 | + protected $fileBefore; |
| 21 | + protected $fileAfter; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param string $fileBefore |
| 25 | + * @param string $fileAfter |
| 26 | + */ |
| 27 | + public function __construct($fileBefore = null, $fileAfter = null) |
| 28 | + { |
| 29 | + $this->fileBefore = $fileBefore; |
| 30 | + $this->fileAfter = $fileAfter; |
| 31 | + } |
| 32 | + |
| 33 | + public function analyze(Class_ $classBefore, Class_ $classAfter) |
| 34 | + { |
| 35 | + $report = new Report(); |
| 36 | + |
| 37 | + $methodsBefore = $classBefore->getMethods(); |
| 38 | + $methodsAfter = $classAfter->getMethods(); |
| 39 | + |
| 40 | + $methodsBeforeKeyed = []; |
| 41 | + foreach ($methodsBefore as $method) { |
| 42 | + $methodsBeforeKeyed[$method->name] = $method; |
| 43 | + } |
| 44 | + |
| 45 | + $methodsAfterKeyed = []; |
| 46 | + foreach ($methodsAfter as $method) { |
| 47 | + $methodsAfterKeyed[$method->name] = $method; |
| 48 | + } |
| 49 | + |
| 50 | + $methodsBeforeKeyed = array_filter($methodsBeforeKeyed, function (ClassMethod $method) { |
| 51 | + return $method->isPublic(); |
| 52 | + }); |
| 53 | + |
| 54 | + $methodsAfterKeyed = array_filter($methodsAfterKeyed, function (ClassMethod $method) { |
| 55 | + return $method->isPublic(); |
| 56 | + }); |
| 57 | + |
| 58 | + $methodNamesBefore = array_keys($methodsBeforeKeyed); |
| 59 | + $methodNamesAfter = array_keys($methodsAfterKeyed); |
| 60 | + $methodsAdded = array_diff($methodNamesAfter, $methodNamesBefore); |
| 61 | + $methodsRemoved = array_diff($methodNamesBefore, $methodNamesAfter); |
| 62 | + $methodsToVerify = array_intersect($methodNamesBefore, $methodNamesAfter); |
| 63 | + |
| 64 | + // Here we only care about public methods as they are the only part of the API we care about |
| 65 | + |
| 66 | + // Removed methods can either be implemented in parent classes or not exist anymore |
| 67 | + foreach ($methodsRemoved as $method) { |
| 68 | + $methodBefore = $methodsBeforeKeyed[$method]; |
| 69 | + $data = new ClassMethodRemoved($this->fileBefore, $classBefore, $methodBefore); |
| 70 | + $report->addClassMethod($data, Level::MAJOR); |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($methodsToVerify as $method) { |
| 74 | + /** @var \PhpParser\Node\Stmt\ClassMethod $methodBefore */ |
| 75 | + $methodBefore = $methodsBeforeKeyed[$method]; |
| 76 | + /** @var \PhpParser\Node\Stmt\ClassMethod $methodAfter */ |
| 77 | + $methodAfter = $methodsAfterKeyed[$method]; |
| 78 | + |
| 79 | + // Leave non-strict comparison here |
| 80 | + if ($methodBefore != $methodAfter) { |
| 81 | + $paramsBefore = $methodBefore->params; |
| 82 | + $paramsAfter = $methodAfter->params; |
| 83 | + // Signature |
| 84 | + |
| 85 | + if ( ! Signature::isSameTypehints($paramsBefore, $paramsAfter)) { |
| 86 | + $data = new ClassMethodParameterChanged($this->fileBefore, $classBefore, $methodBefore, $this->fileAfter, $classAfter, $methodAfter); |
| 87 | + $report->addClassMethod($data, Level::MAJOR); |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + if ( ! Signature::isSameVariables($paramsBefore, $paramsAfter)) { |
| 92 | + $data = new ClassMethodParameterChanged($this->fileBefore, $classBefore, $methodBefore, $this->fileAfter, $classAfter, $methodAfter); |
| 93 | + $report->addClassMethod($data, Level::PATCH); |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + // Different length (considering params with defaults) |
| 98 | + |
| 99 | + // Difference in source code |
| 100 | + if ($methodBefore->stmts != $methodAfter->stmts) { |
| 101 | + $data = new ClassMethodImplementationChanged($this->fileBefore, $classBefore, $methodBefore, $this->fileAfter, $classAfter, $methodAfter); |
| 102 | + $report->addClassMethod($data, Level::PATCH); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + // Unable to match an issue, but there is one... |
| 107 | + $data = new Unknown($this->fileBefore, $this->fileAfter); |
| 108 | + $report->addClassMethod($data, Level::MAJOR); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Added methods implies MINOR BUMP |
| 113 | + foreach ($methodsAdded as $method) { |
| 114 | + $methodAfter = $methodsAfterKeyed[$method]; |
| 115 | + $data = new ClassMethodAdded($this->fileAfter, $classAfter, $methodAfter); |
| 116 | + $report->addClassMethod($data, Level::MINOR); |
| 117 | + } |
| 118 | + |
| 119 | + return $report; |
| 120 | + } |
| 121 | +} |
0 commit comments