| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Superdesk Web Publisher Core Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2016 Sourcefabric z.u. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2016 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace SWP\Bundle\CoreBundle\Locator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SWP\Bundle\CoreBundle\Detection\DeviceDetectionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sylius\Bundle\ThemeBundle\Locator\ResourceLocatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sylius\Bundle\ThemeBundle\Locator\ResourceNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sylius\Bundle\ThemeBundle\Model\ThemeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\HttpKernel\Bundle\BundleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\HttpKernel\KernelInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class BundleResourceLocator implements ResourceLocatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var Filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var KernelInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var DeviceDetectionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     private $deviceDetection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function __construct(Filesystem $filesystem, KernelInterface $kernel, DeviceDetectionInterface $deviceDetection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->filesystem = $filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->kernel = $kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 101 |  |         $this->deviceDetection = $deviceDetection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 101 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 101 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 101 |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 101 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function locateResource(string $resourcePath, ThemeInterface $theme): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->assertResourcePathIsValid($resourcePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (false !== strpos($resourcePath, 'Bundle/Resources/views/')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             // When using bundle notation, we get a path like @AcmeBundle/Resources/views/template.html.twig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 19 |  |             return $this->locateResourceBasedOnBundleNotation($resourcePath, $theme); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 19 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 19 |  |         // When using namespaced Twig paths, we get a path like @Acme/template.html.twig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 19 |  |         return $this->locateResourceBasedOnTwigNamespace($resourcePath, $theme); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 19 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     private function assertResourcePathIsValid(string $resourcePath): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 19 |  |         if (0 !== strpos($resourcePath, '@')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             throw new \InvalidArgumentException(sprintf('Bundle resource path (given "%s") should start with an "@".', $resourcePath)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (false !== strpos($resourcePath, '..')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             throw new \InvalidArgumentException(sprintf('File name "%s" contains invalid characters (..).', $resourcePath)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 19 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 19 |  |     private function locateResourceBasedOnBundleNotation(string $resourcePath, ThemeInterface $theme): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 19 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 19 |  |         $bundleName = substr($resourcePath, 1, strpos($resourcePath, '/') - 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 19 |  |         $resourceName = substr($resourcePath, strpos($resourcePath, 'Resources/') + strlen('Resources/')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 19 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 19 |  |         // Symfony 4.0+ always returns a single bundle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 19 |  |         /** @var BundleInterface|BundleInterface[] $bundles */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 14 |  |         $bundles = $this->kernel->getBundle($bundleName, false); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 19 |  |         // So we need to hack it to support both Symfony 3.4 and Symfony 4.0+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if (!is_array($bundles)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $bundles = [$bundles]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 19 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         foreach ($bundles as $bundle) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 91 |  | View Code Duplication |             if (null !== $this->deviceDetection->getType()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 $path = sprintf('%s/%s/%s/%s', $theme->getPath(), $this->deviceDetection->getType(), $bundle->getName(), $resourceName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 if ($this->filesystem->exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 19 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 19 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $path = sprintf('%s/%s/%s', $theme->getPath(), $bundle->getName(), $resourceName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             if ($this->filesystem->exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 19 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         throw new ResourceNotFoundException($resourcePath, $theme); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 19 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     private function locateResourceBasedOnTwigNamespace(string $resourcePath, ThemeInterface $theme): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 19 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $twigNamespace = substr($resourcePath, 1, strpos($resourcePath, '/') - 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $resourceName = substr($resourcePath, strpos($resourcePath, '/') + 1); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 111 |  | View Code Duplication |         if (null !== $this->deviceDetection->getType()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $path = sprintf('%s/%s/%s/%s', $theme->getPath(), $this->deviceDetection->getType(), $this->getBundleOrPluginName($twigNamespace), $resourceName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             if ($this->filesystem->exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 19 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 19 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $path = sprintf('%s/%s/views/%s', $theme->getPath(), $this->getBundleOrPluginName($twigNamespace), $resourceName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if ($this->filesystem->exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         throw new ResourceNotFoundException($resourcePath, $theme); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 19 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 127 | 19 |  |     private function getBundleOrPluginName(string $twigNamespace): string | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |         if ('Plugin' === substr($twigNamespace, -6)) { | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |             return $twigNamespace; | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |         return $twigNamespace.'Bundle'; | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 135 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  | 
            
                        
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.