|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Magento\SemanticVersionChecker\Analyzer; |
| 11 | + |
| 12 | +use Magento\SemanticVersionChecker\ClassHierarchy\DependencyGraph; |
| 13 | +use Magento\SemanticVersionChecker\Helper\Node; |
| 14 | +use Magento\SemanticVersionChecker\Operation\ClassLikeApiAnnotationAdded; |
| 15 | +use Magento\SemanticVersionChecker\Operation\ClassLikeApiAnnotationRemoved; |
| 16 | +use PhpParser\Node\Stmt\ClassLike; |
| 17 | +use PHPSemVerChecker\Report\Report; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class Extends analyzer performs comparison of classes/interfaces/traits and creates reports such as: |
| 21 | + * <ul> |
| 22 | + * <li><kbd>added</kbd>: @api annotation has been added</li> |
| 23 | + * <li><kbd>remove</kbd>: @api annotation has been removed</li> |
| 24 | + * <ul> |
| 25 | + */ |
| 26 | +class ClassLikeApiAnnotationAnalyzer extends AbstractCodeAnalyzer |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @param null $context |
| 30 | + * @param null $fileBefore |
| 31 | + * @param null $fileAfter |
| 32 | + * @param DependencyGraph|null $dependencyGraph |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + $context = null, |
| 36 | + $fileBefore = null, |
| 37 | + $fileAfter = null, |
| 38 | + DependencyGraph $dependencyGraph = null |
| 39 | + ) { |
| 40 | + parent::__construct($context, $fileBefore, $fileAfter, $dependencyGraph); |
| 41 | + $this->nodeHelper = new Node(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Node |
| 46 | + */ |
| 47 | + private $nodeHelper; |
| 48 | + |
| 49 | + /** |
| 50 | + * Get the name of a ClassLike node |
| 51 | + * |
| 52 | + * @param ClassLike $node |
| 53 | + * @return string |
| 54 | + */ |
| 55 | + protected function getNodeName($node) |
| 56 | + { |
| 57 | + return $node->name->toString(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Use nodes of the ClassLike type for this analyzer |
| 62 | + * |
| 63 | + * @return string |
| 64 | + */ |
| 65 | + protected function getNodeClass() |
| 66 | + { |
| 67 | + return ClassLike::class; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @inheritDoc |
| 72 | + */ |
| 73 | + protected function reportAddedNode($report, $fileAfter, $contextAfter, $nodeAfter) |
| 74 | + { |
| 75 | + // implementation not needed |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @inheritDoc |
| 80 | + */ |
| 81 | + protected function reportRemovedNode($report, $fileBefore, $contextBefore, $nodeBefore) |
| 82 | + { |
| 83 | + // implementation not needed |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Find changes to class/interface @api annotation |
| 88 | + * |
| 89 | + * @param Report $report |
| 90 | + * @param ClassLike $contextBefore |
| 91 | + * @param ClassLike $contextAfter |
| 92 | + * @param string[] $toVerify |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + protected function reportChanged($report, $contextBefore, $contextAfter, $toVerify) |
| 96 | + { |
| 97 | + $isApiBefore = $this->nodeHelper->isApiNode($contextBefore); |
| 98 | + $isApiAfter = $this->nodeHelper->isApiNode($contextAfter); |
| 99 | + |
| 100 | + if (!$isApiBefore && $isApiAfter) { |
| 101 | + $operation = new ClassLikeApiAnnotationAdded($contextAfter, $this->fileAfter); |
| 102 | + $report->add($this->context, $operation); |
| 103 | + } elseif ($isApiBefore && !$isApiAfter) { |
| 104 | + $operation = new ClassLikeApiAnnotationRemoved($contextAfter, $this->fileAfter); |
| 105 | + $report->add($this->context, $operation); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments