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\Compiler; |
||
| 15 | |||
| 16 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
||
| 17 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
| 18 | use Symfony\Component\DependencyInjection\Definition; |
||
| 19 | use Symfony\Component\DependencyInjection\Reference; |
||
| 20 | |||
| 21 | class CoveragePass implements CompilerPassInterface |
||
| 22 | { |
||
| 23 | public function process(ContainerBuilder $container) |
||
| 24 | { |
||
| 25 | $this->processDriver($container); |
||
| 26 | $this->processFilter($container); |
||
| 27 | } |
||
| 28 | |||
| 29 | private function processDriver(ContainerBuilder $container) |
||
| 30 | { |
||
| 31 | $driverClass = $container->get('runtime')->getDriverClass(); |
||
| 32 | $container->getParameterBag()->set('coverage.driver.class', $driverClass); |
||
| 33 | |||
| 34 | $r = new \ReflectionClass($driverClass); |
||
| 35 | if ($r->hasMethod('setFilter')) { |
||
| 36 | $definition = $container->getDefinition('coverage.filter'); |
||
| 37 | $definition->addMethodCall('setFilter', [new Reference('coverage.filter')]); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | private function processFilter(ContainerBuilder $container) |
||
| 42 | { |
||
| 43 | $config = $container->getParameter('config.filter'); |
||
| 44 | $definition = $container->getDefinition('coverage.filter'); |
||
| 45 | |||
| 46 | foreach ($config as $options) { |
||
| 47 | $options['basePath'] = ''; |
||
| 48 | $this->filterWhitelist($definition, $options, 'add'); |
||
| 49 | $exclude = $options['exclude']; |
||
| 50 | foreach ($exclude as $item) { |
||
| 51 | $item['basePath'] = ''; |
||
| 52 | $this->filterWhitelist($definition, $item, 'remove'); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | private function filterWhitelist(Definition $definition, $options, $method) |
||
| 58 | { |
||
| 59 | $basePath = $options['basePath']; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 60 | $suffix = $options['suffix'] ?: '.php'; |
||
| 61 | $prefix = $options['prefix'] ?: ''; |
||
| 62 | $type = $options['directory'] ? 'directory' : 'file'; |
||
| 63 | $directory = $options['directory']; |
||
| 64 | $file = $options['file']; |
||
| 65 | |||
| 66 | if (preg_match('/\/\*(\..+)/', $directory, $matches)) { |
||
| 67 | $suffix = $matches[1]; |
||
| 68 | $directory = str_replace($matches[0], '', $directory); |
||
| 69 | } |
||
| 70 | |||
| 71 | $methodSuffix = 'add' === $method ? 'ToWhitelist' : 'FromWhitelist'; |
||
| 72 | if ('directory' === $type) { |
||
| 73 | $definition->addMethodCall($method.'Directory'.$methodSuffix, [$directory, $suffix, $prefix]); |
||
| 74 | } |
||
| 75 | |||
| 76 | if ('file' === $type) { |
||
| 77 | $definition->addMethodCall($method.'File'.$methodSuffix, [$file]); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 |