|
| 1 | +<?php declare(strict_types=1); # -*- coding: utf-8 -*- |
| 2 | +/* |
| 3 | + * This file is part of the php-coding-standards package. |
| 4 | + * |
| 5 | + * (c) Inpsyde GmbH |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + * |
| 10 | + * This file contains code from "phpcs-neutron-standard" repository |
| 11 | + * found at https://github.com/Automattic/phpcs-neutron-standard |
| 12 | + * Copyright (c) Automattic |
| 13 | + * released under MIT license. |
| 14 | + */ |
| 15 | + |
| 16 | +namespace Inpsyde\InpsydeCodingStandard\Sniffs\CodeQuality; |
| 17 | + |
| 18 | +use Inpsyde\InpsydeCodingStandard\Helpers; |
| 19 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 20 | +use PHP_CodeSniffer\Files\File; |
| 21 | + |
| 22 | +class HookClosureReturnSniff implements Sniff |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @inheritdoc |
| 26 | + */ |
| 27 | + public function register() |
| 28 | + { |
| 29 | + return [T_CLOSURE]; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritdoc |
| 34 | + */ |
| 35 | + public function process(File $file, $position) |
| 36 | + { |
| 37 | + if (!Helpers::isHookClosure($file, $position)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + list($functionStart, $functionEnd) = Helpers::functionBoundaries($file, $position); |
| 42 | + if (!$functionStart < 0 || $functionEnd <= 0) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + list($nonVoidReturnCount, $voidReturnCount) = Helpers::countReturns($file, $position); |
| 47 | + |
| 48 | + $isFilterClosure = Helpers::isHookClosure($file, $position, true, false); |
| 49 | + |
| 50 | + if ($isFilterClosure && (!$nonVoidReturnCount || $voidReturnCount)) { |
| 51 | + $file->addError( |
| 52 | + 'No (or void) return from filter closure.', |
| 53 | + $position, |
| 54 | + 'NoReturnFromFilter' |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + if (!$isFilterClosure && $nonVoidReturnCount) { |
| 59 | + $file->addError('Return value from action closure.', $position, 'ReturnFromAction'); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments