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 | |||
| 12 | declare(strict_types=1); |
||
| 13 | /* |
||
| 14 | * This file is a part of graphql-youshido project. |
||
| 15 | * |
||
| 16 | * @author Alexandr Viniychuk <[email protected]> |
||
| 17 | * created: 11/28/15 12:05 PM |
||
| 18 | */ |
||
| 19 | |||
| 20 | namespace Youshido\Tests\DataProvider; |
||
| 21 | |||
| 22 | class TestScalarDataProvider |
||
| 23 | { |
||
| 24 | public static function getsList() |
||
| 25 | { |
||
| 26 | return [ |
||
| 27 | 'Int', |
||
| 28 | 'String', |
||
| 29 | 'Boolean', |
||
| 30 | 'Float', |
||
| 31 | 'Id', |
||
| 32 | ]; |
||
| 33 | } |
||
| 34 | |||
| 35 | public static function getIntTestData() |
||
| 36 | { |
||
| 37 | /* input, serialization, isValid */ |
||
| 38 | return [ |
||
| 39 | [1, 1, true], |
||
| 40 | [0, 0, true], |
||
| 41 | [-1, -1, true], |
||
| 42 | [0.1, 0, false], |
||
| 43 | [1.1, 1, false], |
||
| 44 | [[1], 1, false], |
||
| 45 | [1e5, 100000, false], |
||
| 46 | ['1', 1, false], |
||
| 47 | [PHP_INT_MAX - 1, PHP_INT_MAX - 1, true], |
||
| 48 | [~PHP_INT_MAX + 1, ~PHP_INT_MAX + 1, true], |
||
| 49 | [1e100, null, false], |
||
| 50 | [-1e100, null, false], |
||
| 51 | ['-1.1', -1, false], |
||
| 52 | ['one', null, false], |
||
| 53 | [null, null, true], |
||
| 54 | [false, 0, false], |
||
| 55 | [true, 1, false], |
||
| 56 | ]; |
||
| 57 | } |
||
| 58 | |||
| 59 | public static function getFloatTestData() |
||
| 60 | { |
||
| 61 | return [ |
||
| 62 | [1, 1.0, true], |
||
| 63 | [0, 0.0, true], |
||
| 64 | [-1, -1, true], |
||
| 65 | [0.1, 0.1, true], |
||
| 66 | [1.1, 1.1, true], |
||
| 67 | ['-1.1', -1.1, false], |
||
| 68 | ['one', null, false], |
||
| 69 | [false, 0.0, false], |
||
| 70 | [null, null, true], |
||
| 71 | [true, 1.0, false], |
||
| 72 | ]; |
||
| 73 | } |
||
| 74 | |||
| 75 | public static function getStringTestData() |
||
| 76 | { |
||
| 77 | return [ |
||
| 78 | ['string', 'string', true], |
||
| 79 | [1, '1', true], |
||
| 80 | [1.1, '1.1', true], |
||
| 81 | [null, null, true], |
||
| 82 | [true, 'true', true], |
||
| 83 | [false, 'false', true], |
||
| 84 | [[], null, false], |
||
| 85 | ]; |
||
| 86 | } |
||
| 87 | |||
| 88 | public static function getBooleanTestData() |
||
| 89 | { |
||
| 90 | return [ |
||
| 91 | ['string', true, false], |
||
| 92 | ['', false, false], |
||
| 93 | [1, true, false], |
||
| 94 | [0, false, false], |
||
| 95 | [null, false, true], |
||
| 96 | [true, true, true], |
||
| 97 | [false, false, true], |
||
| 98 | [null, null, true], |
||
| 99 | ['true', true, false], |
||
| 100 | ['false', false, false], |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | |||
| 104 | public static function getIdTestData() |
||
| 105 | { |
||
| 106 | return [ |
||
| 107 | ['string-id', 'string-id', true], |
||
| 108 | ['', null, true], |
||
| 109 | [1, '1', true], |
||
| 110 | ]; |
||
| 111 | } |
||
| 112 | |||
| 113 | public static function getDatetimeTestData() |
||
| 114 | { |
||
| 115 | $time = \time(); |
||
| 116 | |||
| 117 | return [ |
||
| 118 | [null, null, true], |
||
| 119 | [(new \DateTime())->setTimestamp($time), \date('Y-m-d H:i:s', $time), true], |
||
| 120 | ]; |
||
| 121 | } |
||
| 122 | |||
| 123 | public static function getDatetimetzTestData() |
||
| 124 | { |
||
| 125 | $time = \time(); |
||
| 126 | |||
| 127 | return [ |
||
| 128 | [null, null, true], |
||
| 129 | [(new \DateTime())->setTimestamp($time), \date('r', $time), true], |
||
| 130 | ]; |
||
| 131 | } |
||
| 132 | |||
| 133 | public static function getDateTestData() |
||
| 134 | { |
||
| 135 | $time = \time(); |
||
| 136 | |||
| 137 | return [ |
||
| 138 | [null, null, true], |
||
| 139 | [(new \DateTime())->setTimestamp($time), \date('Y-m-d', $time), true], |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | |||
| 153 |