Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 11 | class MiddlewareBuilder |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * TokenService for building. |
||
| 15 | * |
||
| 16 | * @var TokenServiceInterface |
||
| 17 | */ |
||
| 18 | protected $tokenService; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Middleware class. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $middlewareClass; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Create a new MiddlewareBuilder. |
||
| 29 | * |
||
| 30 | * @param TokenService $tokenService TokenService for building |
||
| 31 | * @param string $middlewareClass Middleware class |
||
| 32 | */ |
||
| 33 | public function __construct(TokenService $tokenService, $middlewareClass = Middleware::class) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Create a new MiddlewareBuilder. |
||
| 41 | * |
||
| 42 | * `$ttl` is used for calculating the expiration time of the tokens, its default value (1440sec === 24min) |
||
| 43 | * correspond to the default `session.gc_maxlifetime`. |
||
| 44 | * |
||
| 45 | * @see http://php.net/manual/en/session.configuration.php Documentation of `session.gc-maxlifetime` |
||
| 46 | * |
||
| 47 | * @param string $key Shared secret key used for generating token signatures |
||
| 48 | * @param int $ttl Default Time to Live in seconds |
||
| 49 | * @param string $algo Name of hashing algorithm. See hash_algos() for a list of supported algorithms |
||
| 50 | * @param string $middlewareClass Middleware class |
||
| 51 | * |
||
| 52 | * @return static |
||
| 53 | */ |
||
| 54 | public static function create( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Build a AngularJS compatible stateless Cookie-To-Header CSRF proptection middleware. |
||
| 65 | * |
||
| 66 | * + Sends tokens via cookies |
||
| 67 | * + Accepts tokens via request headers |
||
| 68 | * + Always accepts GET requests |
||
| 69 | * |
||
| 70 | * @param string $cookieName Cookie name |
||
| 71 | * @param string $headerName Header field name |
||
| 72 | * @param callable $rejectMiddleware See `\Schnittstabil\Psr7\Csrf\Middlewares\Guard` for details |
||
| 73 | * @param callable $cookieModifier See `Schnittstabil\Psr7\Csrf\Middlewares\RespondWithCookieToken` for details |
||
| 74 | * |
||
| 75 | * @return static |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function buildCookieToHeaderMiddleware( |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Build a stateless Header-To-Header CSRF proptection middleware. |
||
| 92 | * |
||
| 93 | * + Sends tokens via headers |
||
| 94 | * + Accepts tokens via request headers |
||
| 95 | * + Always accepts GET requests |
||
| 96 | * |
||
| 97 | * @param string $responseHeaderName Response header field name |
||
| 98 | * @param string $requestHeaderName Request header field name |
||
| 99 | * @param callable $rejectMiddleware See `\Schnittstabil\Psr7\Csrf\Middlewares\Guard` for details |
||
| 100 | * |
||
| 101 | * @return static |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function buildHeaderToHeaderMiddleware( |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Build a stateless Synchronizer Token Pattern CSRF proptection middleware. |
||
| 117 | * |
||
| 118 | * + Accepts tokens via request body (`ServerRequestInterface::getParsedBody`) |
||
| 119 | * + Always accepts GET requests |
||
| 120 | * + Tokens have to be generated by `getTokenService()->generate()` and manually rendered into HTML/JSON or XML. |
||
| 121 | * |
||
| 122 | * |
||
| 123 | * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValue` |
||
| 124 | * @see http://www.php-fig.org/psr/psr-7 Documentation of `ServerRequestInterface::getParsedBody` |
||
| 125 | * |
||
| 126 | * @param string|int|mixed[] $path a `Schnittstabil\Get\getValue` path |
||
| 127 | * @param callable $rejectMiddleware See `\Schnittstabil\Psr7\Csrf\Middlewares\Guard` for details |
||
| 128 | * |
||
| 129 | * @return static |
||
| 130 | */ |
||
| 131 | public function buildSynchronizerTokenPatternMiddleware( |
||
| 140 | } |
||
| 141 |