| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Superdesk Web Publisher Core Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright 2017 Sourcefabric z.u. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright 2017 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace SWP\Bundle\CoreBundle\Twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use SWP\Bundle\CoreBundle\Model\ArticleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SWP\Component\TemplatesSystem\Gimme\Meta\Meta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Twig\Extension\AbstractExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Twig\TwigFunction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class ArticleEventsExtension extends AbstractExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return TwigFunction[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function getFunctions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             new TwigFunction('countPageView', [$this, 'renderPageViewCount'], ['is_safe' => ['html']]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             new TwigFunction('countArticlesImpressions', [$this, 'renderLinkImpressionCount'], ['is_safe' => ['html']]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param Meta $meta | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return string|void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function renderLinkImpressionCount() | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $jsTemplate = <<<'EOT' | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | <script type="text/javascript"> | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | var arr = [], l = document.links; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | for(var i=0; i<l.length; i++) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |   if(arr.indexOf(l[i].href) === -1){arr.push(l[i].href);} | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | } | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | var xhr = new XMLHttpRequest(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | var read_date = new Date(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | var request_randomizer = "&" + read_date.getTime() + Math.random(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | xhr.open('POST', '/_swp_analytics?type=impression'+request_randomizer); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | xhr.setRequestHeader("Content-Type", "application/json"); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | xhr.send(JSON.stringify(arr)); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | </script> | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | EOT; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         return $jsTemplate; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param Meta $meta | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return string|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function renderPageViewCount(Meta $meta = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (null === $meta) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $jsTemplate = <<<'EOT' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | <script type="text/javascript"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | var xhr = new XMLHttpRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | var read_date = new Date(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | var request_randomizer = "&" + read_date.getTime() + Math.random(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | xhr.open('GET', '/_swp_analytics?articleId=%s'+request_randomizer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | xhr.send(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | </script> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | EOT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $article = $meta->getValues(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (!$article instanceof ArticleInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return sprintf($jsTemplate, $article->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                        
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.