doyolabs /
code-coverage-bridge
| 1 | <?php |
||||
| 2 | |||||
| 3 | /* |
||||
| 4 | * This file is part of the doyo/code-coverage project. |
||||
| 5 | * |
||||
| 6 | * (c) Anthonius Munthi <[email protected]> |
||||
| 7 | * |
||||
| 8 | * For the full copyright and license information, please view the LICENSE |
||||
| 9 | * file that was distributed with this source code. |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | declare(strict_types=1); |
||||
| 13 | |||||
| 14 | namespace Doyo\Bridge\CodeCoverage\Session; |
||||
| 15 | |||||
| 16 | use Doyo\Bridge\CodeCoverage\Exception\SessionException; |
||||
| 17 | use Doyo\Bridge\CodeCoverage\Processor; |
||||
| 18 | use Doyo\Bridge\CodeCoverage\ProcessorInterface; |
||||
| 19 | use Doyo\Bridge\CodeCoverage\TestCase; |
||||
| 20 | use SebastianBergmann\CodeCoverage\CodeCoverage; |
||||
|
0 ignored issues
–
show
|
|||||
| 21 | use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
||||
|
0 ignored issues
–
show
The type
Symfony\Component\Cache\Adapter\FilesystemAdapter was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 22 | |||||
| 23 | abstract class Session implements \Serializable, SessionInterface |
||||
| 24 | { |
||||
| 25 | const CACHE_KEY = 'session'; |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * @var string|null |
||||
| 29 | */ |
||||
| 30 | protected $name; |
||||
| 31 | |||||
| 32 | /** |
||||
| 33 | * @var TestCase |
||||
| 34 | */ |
||||
| 35 | protected $testCase; |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * @var FilesystemAdapter|null |
||||
| 39 | */ |
||||
| 40 | protected $adapter; |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * @var array |
||||
| 44 | */ |
||||
| 45 | protected $data = []; |
||||
| 46 | |||||
| 47 | /** |
||||
| 48 | * @var ProcessorInterface |
||||
| 49 | */ |
||||
| 50 | protected $processor; |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * @var \Exception[] |
||||
| 54 | */ |
||||
| 55 | protected $exceptions = []; |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * @var bool |
||||
| 59 | */ |
||||
| 60 | protected $hasStarted = false; |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * Code coverage for this session. |
||||
| 64 | * |
||||
| 65 | * @var CodeCoverage |
||||
| 66 | */ |
||||
| 67 | protected $codeCoverage; |
||||
| 68 | |||||
| 69 | protected $patchXdebug = true; |
||||
| 70 | |||||
| 71 | public function __construct($name, $patchXdebug = true) |
||||
| 72 | { |
||||
| 73 | $dir = sys_get_temp_dir().'/doyo/behat-coverage-extension'; |
||||
| 74 | $adapter = new FilesystemAdapter($name, 0, $dir); |
||||
| 75 | $this->adapter = $adapter; |
||||
| 76 | $this->name = $name; |
||||
| 77 | $this->patchXdebug = $patchXdebug; |
||||
| 78 | $this->refresh(); |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | /** |
||||
| 82 | * {@inheritdoc} |
||||
| 83 | */ |
||||
| 84 | public function reset() |
||||
| 85 | { |
||||
| 86 | $this->testCase = null; |
||||
| 87 | $this->exceptions = []; |
||||
| 88 | $this->processor->clear(); |
||||
| 89 | |||||
| 90 | $this->save(); |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | /** |
||||
| 94 | * {@inheritdoc} |
||||
| 95 | */ |
||||
| 96 | public function serialize() |
||||
| 97 | { |
||||
| 98 | $data = [ |
||||
| 99 | $this->name, |
||||
| 100 | $this->testCase, |
||||
| 101 | $this->exceptions, |
||||
| 102 | $this->processor, |
||||
| 103 | $this->patchXdebug, |
||||
| 104 | ]; |
||||
| 105 | |||||
| 106 | return serialize($data); |
||||
| 107 | } |
||||
| 108 | |||||
| 109 | /** |
||||
| 110 | * {@inheritdoc} |
||||
| 111 | */ |
||||
| 112 | public function unserialize($serialized) |
||||
| 113 | { |
||||
| 114 | list( |
||||
| 115 | $this->name, |
||||
| 116 | $this->testCase, |
||||
| 117 | $this->exceptions, |
||||
| 118 | $this->processor, |
||||
| 119 | $this->patchXdebug |
||||
| 120 | ) = unserialize($serialized); |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | /** |
||||
| 124 | * {@inheritdoc} |
||||
| 125 | */ |
||||
| 126 | public function refresh() |
||||
| 127 | { |
||||
| 128 | $adapter = $this->adapter; |
||||
| 129 | $cached = $adapter->getItem(static::CACHE_KEY)->get(); |
||||
|
0 ignored issues
–
show
The method
getItem() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 130 | |||||
| 131 | if ($cached instanceof self) { |
||||
| 132 | $this->name = $cached->getName(); |
||||
| 133 | $this->testCase = $cached->getTestCase(); |
||||
| 134 | $this->exceptions = $cached->getExceptions(); |
||||
| 135 | $this->processor = $cached->getProcessor(); |
||||
| 136 | $this->patchXdebug = $cached->getPatchXdebug(); |
||||
| 137 | } |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | public function setPatchXdebug(bool $flag) |
||||
| 141 | { |
||||
| 142 | $this->patchXdebug = $flag; |
||||
| 143 | } |
||||
| 144 | |||||
| 145 | /** |
||||
| 146 | * @return bool |
||||
| 147 | */ |
||||
| 148 | public function getPatchXdebug(): bool |
||||
| 149 | { |
||||
| 150 | return $this->patchXdebug; |
||||
| 151 | } |
||||
| 152 | |||||
| 153 | /** |
||||
| 154 | * @return string|null |
||||
| 155 | */ |
||||
| 156 | public function getName() |
||||
| 157 | { |
||||
| 158 | return $this->name; |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | /** |
||||
| 162 | * @return TestCase |
||||
| 163 | */ |
||||
| 164 | public function getTestCase() |
||||
| 165 | { |
||||
| 166 | return $this->testCase; |
||||
| 167 | } |
||||
| 168 | |||||
| 169 | /** |
||||
| 170 | * {@inheritdoc} |
||||
| 171 | */ |
||||
| 172 | public function setTestCase(TestCase $testCase = null) |
||||
| 173 | { |
||||
| 174 | $this->testCase = $testCase; |
||||
| 175 | |||||
| 176 | return $this; |
||||
| 177 | } |
||||
| 178 | |||||
| 179 | /** |
||||
| 180 | * @return FilesystemAdapter|null |
||||
| 181 | */ |
||||
| 182 | public function getAdapter(): FilesystemAdapter |
||||
| 183 | { |
||||
| 184 | return $this->adapter; |
||||
|
0 ignored issues
–
show
|
|||||
| 185 | } |
||||
| 186 | |||||
| 187 | /** |
||||
| 188 | * @param FilesystemAdapter|null $adapter |
||||
| 189 | */ |
||||
| 190 | public function setAdapter(FilesystemAdapter $adapter) |
||||
| 191 | { |
||||
| 192 | $this->adapter = $adapter; |
||||
| 193 | } |
||||
| 194 | |||||
| 195 | /** |
||||
| 196 | * @param ProcessorInterface $processor |
||||
| 197 | */ |
||||
| 198 | public function setProcessor(ProcessorInterface $processor = null) |
||||
| 199 | { |
||||
| 200 | $this->processor = $processor; |
||||
| 201 | } |
||||
| 202 | |||||
| 203 | /** |
||||
| 204 | * @return ProcessorInterface|null |
||||
| 205 | */ |
||||
| 206 | public function getProcessor() |
||||
| 207 | { |
||||
| 208 | return $this->processor; |
||||
| 209 | } |
||||
| 210 | |||||
| 211 | /** |
||||
| 212 | * {@inheritdoc} |
||||
| 213 | */ |
||||
| 214 | public function hasExceptions() |
||||
| 215 | { |
||||
| 216 | return \count($this->exceptions) > 0; |
||||
| 217 | } |
||||
| 218 | |||||
| 219 | /** |
||||
| 220 | * {@inheritdoc} |
||||
| 221 | */ |
||||
| 222 | public function getExceptions() |
||||
| 223 | { |
||||
| 224 | return $this->exceptions; |
||||
| 225 | } |
||||
| 226 | |||||
| 227 | /** |
||||
| 228 | * @param \Exception $e |
||||
| 229 | */ |
||||
| 230 | public function addException(\Exception $e) |
||||
| 231 | { |
||||
| 232 | $id = md5($e->getMessage()); |
||||
| 233 | $this->exceptions[$id] = $e; |
||||
| 234 | } |
||||
| 235 | |||||
| 236 | /** |
||||
| 237 | * @codeCoverageIgnore |
||||
| 238 | */ |
||||
| 239 | public function xdebugPatch() |
||||
| 240 | { |
||||
| 241 | if (!$this->patchXdebug) { |
||||
| 242 | return; |
||||
| 243 | } |
||||
| 244 | |||||
| 245 | $filter = $this->getProcessor()->getCodeCoverageFilter(); |
||||
| 246 | $options = $filter->getWhitelistedFiles(); |
||||
| 247 | $filterKey = 'whitelistedFiles'; |
||||
| 248 | |||||
| 249 | if ( |
||||
| 250 | !\extension_loaded('xdebug') |
||||
| 251 | || !\function_exists('xdebug_set_filter') |
||||
| 252 | || !isset($options[$filterKey]) |
||||
| 253 | ) { |
||||
| 254 | return; |
||||
| 255 | } |
||||
| 256 | |||||
| 257 | $dirs = []; |
||||
| 258 | foreach ($options[$filterKey] as $fileName => $status) { |
||||
| 259 | $dir = \dirname($fileName); |
||||
| 260 | if (!\in_array($dir, $dirs, true)) { |
||||
| 261 | $dirs[] = $dir; |
||||
| 262 | } |
||||
| 263 | } |
||||
| 264 | |||||
| 265 | xdebug_set_filter( |
||||
| 266 | XDEBUG_FILTER_CODE_COVERAGE, |
||||
|
0 ignored issues
–
show
|
|||||
| 267 | XDEBUG_PATH_WHITELIST, |
||||
|
0 ignored issues
–
show
|
|||||
| 268 | $dirs |
||||
| 269 | ); |
||||
| 270 | } |
||||
| 271 | |||||
| 272 | /** |
||||
| 273 | * {@inheritdoc} |
||||
| 274 | */ |
||||
| 275 | final public function start($driver = null) |
||||
| 276 | { |
||||
| 277 | if (null === $this->testCase) { |
||||
| 278 | return; |
||||
| 279 | } |
||||
| 280 | try { |
||||
| 281 | $this->hasStarted = false; |
||||
| 282 | $this->codeCoverage = $this->createCodeCoverage($driver); |
||||
| 283 | $this->xdebugPatch(); |
||||
| 284 | $this->codeCoverage->start($this->testCase->getName()); |
||||
| 285 | $this->hasStarted = true; |
||||
| 286 | } catch (\Exception $e) { |
||||
| 287 | $message = sprintf( |
||||
| 288 | "Can not start code coverage with error message:\n%s", |
||||
| 289 | $e->getMessage() |
||||
| 290 | ); |
||||
| 291 | $exception = new SessionException($message); |
||||
| 292 | throw $exception; |
||||
| 293 | } |
||||
| 294 | } |
||||
| 295 | |||||
| 296 | public function stop() |
||||
| 297 | { |
||||
| 298 | try { |
||||
| 299 | $codeCoverage = $this->codeCoverage; |
||||
| 300 | $processor = $this->processor; |
||||
| 301 | |||||
| 302 | $codeCoverage->stop(); |
||||
| 303 | |||||
| 304 | $processor->merge($codeCoverage); |
||||
| 305 | } catch (\Exception $e) { |
||||
| 306 | throw new SessionException( |
||||
| 307 | sprintf( |
||||
| 308 | 'Failed to stop coverage for session %s: %s', |
||||
| 309 | $this->name, |
||||
| 310 | $e->getMessage() |
||||
| 311 | ) |
||||
| 312 | ); |
||||
| 313 | } |
||||
| 314 | $this->hasStarted = false; |
||||
| 315 | } |
||||
| 316 | |||||
| 317 | /** |
||||
| 318 | * {@inheritdoc} |
||||
| 319 | */ |
||||
| 320 | public function save() |
||||
| 321 | { |
||||
| 322 | $adapter = $this->adapter; |
||||
| 323 | $item = $adapter->getItem(static::CACHE_KEY); |
||||
| 324 | |||||
| 325 | $item->set($this); |
||||
| 326 | $adapter->save($item); |
||||
| 327 | } |
||||
| 328 | |||||
| 329 | public function shutdown() |
||||
| 330 | { |
||||
| 331 | if ($this->hasStarted && null !== $this->processor) { |
||||
| 332 | try { |
||||
| 333 | $this->stop(); |
||||
| 334 | } catch (\Exception $e) { |
||||
| 335 | $this->addException(new SessionException($e->getMessage())); |
||||
| 336 | } |
||||
| 337 | } |
||||
| 338 | $this->hasStarted = false; |
||||
| 339 | $this->save(); |
||||
| 340 | } |
||||
| 341 | |||||
| 342 | /** |
||||
| 343 | * @param mixed|null $driver |
||||
| 344 | * |
||||
| 345 | * @return CodeCoverage |
||||
| 346 | */ |
||||
| 347 | protected function createCodeCoverage($driver): CodeCoverage |
||||
| 348 | { |
||||
| 349 | $filter = $this->processor->getCodeCoverageFilter(); |
||||
| 350 | $options = $this->processor->getCodeCoverageOptions(); |
||||
| 351 | $coverage = new CodeCoverage($driver, $filter); |
||||
| 352 | foreach ($options as $method => $option) { |
||||
| 353 | if (method_exists($coverage, $method)) { |
||||
| 354 | $method = 'set'.ucfirst($method); |
||||
| 355 | \call_user_func_array([$coverage, $method], [$option]); |
||||
| 356 | } |
||||
| 357 | } |
||||
| 358 | |||||
| 359 | return $coverage; |
||||
| 360 | } |
||||
| 361 | } |
||||
| 362 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths