Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Issue.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader;
4
5 use cebe\markdown\Markdown;
6 use Drupal\Component\Plugin\PluginInspectionInterface;
7 use Pharborist\Node;
8
9 class Issue implements IssueInterface {
10
11   /**
12    * @var TargetInterface
13    */
14   protected $target;
15
16   /**
17    * @var string
18    */
19   protected $title;
20
21   /**
22    * @var string
23    */
24   protected $summary;
25
26   /**
27    * @var array
28    */
29   protected $documentation = [];
30
31   /**
32    * @var array
33    */
34   protected $violations = [];
35
36   /**
37    * @var AnalyzerInterface[]
38    */
39   protected $detectors = [];
40
41   /**
42    * @var mixed[]
43    */
44   protected $tags = [];
45
46   /**
47    * @var array[]
48    */
49   protected $fixes = [];
50
51   /**
52    * @var \cebe\markdown\Markdown
53    */
54   protected $parser;
55
56   public function __construct(Target $target, $title, $summary = NULL) {
57     $this->target = $target;
58     $this->setTitle($title);
59
60     if (isset($summary)) {
61       $this->setSummary($summary);
62     }
63
64     $this->parser = new Markdown();
65     $this->parser->html5 = TRUE;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getTitle() {
72     return $this->parser->parseParagraph($this->title);
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function setTitle($title) {
79     $this->title = (string) $title;
80     return $this;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getSummary() {
87     return $this->parser->parse($this->summary);
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function setSummary($summary) {
94     $this->summary = (string) $summary;
95     return $this;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function addDocumentation($url, $title) {
102     $this->documentation[] = [
103       'url' => $url,
104       'title' => $this->parser->parseParagraph($title),
105     ];
106     return $this;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function getDocumentation() {
113     return $this->documentation;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function addAffectedFile($file, AnalyzerInterface $detector) {
120     if (empty($this->violations[$file])) {
121       $this->violations[$file] = [];
122     }
123     $this->addDetector($detector);
124     return $this;
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function addViolation(Node $node, AnalyzerInterface $detector) {
131     $file = $node->getFilename();
132     if ($file) {
133       $this->violations[$file][] = [
134         'line_number' => $node->getLineNumber(),
135       ];
136     }
137     else {
138       throw new \DomainException('Cannot record an issue violation from a detached node.');
139     }
140     $this->addDetector($detector);
141
142     return $this;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function getViolations() {
149     $return_violations = [];
150
151     foreach ($this->violations as $file => $file_violations) {
152       if ($file_violations) {
153         foreach ($file_violations as $violation) {
154           $violation['file'] = $file;
155           $return_violations[] = $violation;
156         }
157       }
158       else {
159         $return_violations[] = ['file' => $file];
160       }
161     }
162
163     return $return_violations;
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function getDetectors() {
170     return array_unique($this->detectors);
171   }
172
173   /**
174    * {@inheritdoc}
175    */
176   public function hasTag($tag) {
177     return array_key_exists($tag, $this->tags);
178   }
179
180   /**
181    * {@inheritdoc}
182    */
183   public function getTag($tag) {
184     return $this->tags[$tag];
185   }
186
187   /**
188    * {@inheritdoc}
189    */
190   public function setTag($tag, $value) {
191     $this->tags[$tag] = $value;
192     return $this;
193   }
194
195   /**
196    * {@inheritdoc}
197    */
198   public function clearTag($tag) {
199     unset($this->tags[$tag]);
200     return $this;
201   }
202
203   /**
204    * {@inheritdoc}
205    */
206   public function getFixes() {
207     return $this->fixes;
208   }
209
210   /**
211    * {@inheritdoc}
212    */
213   public function addFix($fixer_id, array $configuration = []) {
214     $this->fixes[] = array_merge($configuration, ['_plugin_id' => $fixer_id]);
215     $this->setTag('fixable', TRUE);
216     return $this;
217   }
218
219   /**
220    * Stores a reference to an issue detector, if we don't already know about it,
221    * for use by getDetectors().
222    *
223    * @param AnalyzerInterface $detector
224    */
225   protected function addDetector(AnalyzerInterface $detector) {
226     if ($detector instanceof PluginInspectionInterface) {
227       $this->detectors[] = $detector->getPluginId();
228     }
229   }
230
231 }