Completed
Pull Request — master (#966)
by Jeroen De
61:58
created
contexts/PaymentContext/src/Domain/ChecksumGenerator.php 1 patch
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare( strict_types = 1 );
3
+declare(strict_types = 1);
4 4
 
5 5
 namespace WMDE\Fundraising\Frontend\PaymentContext\Domain;
6 6
 
@@ -14,8 +14,8 @@  discard block
 block discarded – undo
14 14
 	/**
15 15
 	 * @param array $checksumCharacters Characters that can be used for the checksum
16 16
 	 */
17
-	public function __construct( array $checksumCharacters ) {
18
-		if ( count( $checksumCharacters ) < 2 ) {
17
+	public function __construct(array $checksumCharacters) {
18
+		if (count($checksumCharacters) < 2) {
19 19
 			throw new \InvalidArgumentException(
20 20
 				'Need at least two characters to create meaningful checksum'
21 21
 			);
@@ -29,14 +29,14 @@  discard block
 block discarded – undo
29 29
 	 *
30 30
 	 * @return string The checksum as a single character present in the constructors array argument
31 31
 	 */
32
-	public function createChecksum( string $string ): string {
33
-		$checksum = hexdec( substr( md5( $this->normalizeString( $string ) ), 0, 8 ) );
32
+	public function createChecksum(string $string): string {
33
+		$checksum = hexdec(substr(md5($this->normalizeString($string)), 0, 8));
34 34
 
35
-		return $this->checksumCharacters[$checksum % count( $this->checksumCharacters )];
35
+		return $this->checksumCharacters[$checksum % count($this->checksumCharacters)];
36 36
 	}
37 37
 
38
-	private function normalizeString( string $string ): string {
39
-		return strtoupper( str_replace( [ '-', '_', ' ' ], [ '', '', '' ], $string ) );
38
+	private function normalizeString(string $string): string {
39
+		return strtoupper(str_replace(['-', '_', ' '], ['', '', ''], $string));
40 40
 	}
41 41
 
42 42
 }
43 43
\ No newline at end of file
Please login to merge, or discard this patch.
contexts/PaymentContext/tests/Unit/Domain/ChecksumGeneratorTest.php 1 patch
Spacing   +35 added lines, -35 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare( strict_types = 1 );
3
+declare(strict_types = 1);
4 4
 
5 5
 namespace WMDE\Fundraising\Frontend\PaymentContext\Tests\Unit\Domain;
6 6
 
@@ -15,62 +15,62 @@  discard block
 block discarded – undo
15 15
 class ChecksumGeneratorTest extends TestCase {
16 16
 
17 17
 	public function testCannotConstructWithLessThanTwoCharacters(): void {
18
-		$this->expectException( \InvalidArgumentException::class );
19
-		new ChecksumGenerator( [ 'a' ] );
18
+		$this->expectException(\InvalidArgumentException::class);
19
+		new ChecksumGenerator(['a']);
20 20
 	}
21 21
 
22 22
 	public function testCanGenerateChecksumWithTwoCharacters(): void {
23
-		$generator = new ChecksumGenerator( [ 'a', 'b' ] );
23
+		$generator = new ChecksumGenerator(['a', 'b']);
24 24
 
25
-		$this->assertSame( 'b', $generator->createChecksum( 'aaaa' ) );
26
-		$this->assertSame( 'b', $generator->createChecksum( 'aaaaa' ) );
25
+		$this->assertSame('b', $generator->createChecksum('aaaa'));
26
+		$this->assertSame('b', $generator->createChecksum('aaaaa'));
27 27
 	}
28 28
 
29 29
 	public function testCanGenerateChecksumWithManyCharacters(): void {
30
-		$generator = new ChecksumGenerator( str_split( 'ACDEFKLMNPRSTWXYZ349' ) );
30
+		$generator = new ChecksumGenerator(str_split('ACDEFKLMNPRSTWXYZ349'));
31 31
 
32
-		$this->assertSame( 'X', $generator->createChecksum( 'AAAU' ) );
33
-		$this->assertSame( 'K', $generator->createChecksum( 'AAAA' ) );
34
-		$this->assertSame( 'C', $generator->createChecksum( 'QAQA' ) );
35
-		$this->assertSame( 'D', $generator->createChecksum( 'ABCD' ) );
32
+		$this->assertSame('X', $generator->createChecksum('AAAU'));
33
+		$this->assertSame('K', $generator->createChecksum('AAAA'));
34
+		$this->assertSame('C', $generator->createChecksum('QAQA'));
35
+		$this->assertSame('D', $generator->createChecksum('ABCD'));
36 36
 	}
37 37
 
38 38
 	public function testIgnoresDashesUnderscoresAndSpaces(): void {
39
-		$generator = new ChecksumGenerator( str_split( 'ACDEFKLMNPRSTWXYZ349' ) );
39
+		$generator = new ChecksumGenerator(str_split('ACDEFKLMNPRSTWXYZ349'));
40 40
 
41
-		$checksum = $generator->createChecksum( 'CAT' );
41
+		$checksum = $generator->createChecksum('CAT');
42 42
 
43
-		$this->assertSame( $checksum, $generator->createChecksum( 'C-AT-' ) );
44
-		$this->assertSame( $checksum, $generator->createChecksum( '_CAT_' ) );
45
-		$this->assertSame( $checksum, $generator->createChecksum( 'C A T' ) );
43
+		$this->assertSame($checksum, $generator->createChecksum('C-AT-'));
44
+		$this->assertSame($checksum, $generator->createChecksum('_CAT_'));
45
+		$this->assertSame($checksum, $generator->createChecksum('C A T'));
46 46
 	}
47 47
 
48 48
 	public function testLowerAndMixedCaseProduceConsistentChecksum(): void {
49
-		$generator = new ChecksumGenerator( str_split( 'ACDEFKLMNPRSTWXYZ349' ) );
49
+		$generator = new ChecksumGenerator(str_split('ACDEFKLMNPRSTWXYZ349'));
50 50
 
51
-		$this->assertSame( 'X', $generator->createChecksum( 'AAAU' ) );
52
-		$this->assertSame( 'X', $generator->createChecksum( 'aAAU' ) );
51
+		$this->assertSame('X', $generator->createChecksum('AAAU'));
52
+		$this->assertSame('X', $generator->createChecksum('aAAU'));
53 53
 
54
-		$this->assertSame( 'K', $generator->createChecksum( 'aAaa' ) );
55
-		$this->assertSame( 'K', $generator->createChecksum( 'aaaa' ) );
54
+		$this->assertSame('K', $generator->createChecksum('aAaa'));
55
+		$this->assertSame('K', $generator->createChecksum('aaaa'));
56 56
 
57
-		$this->assertSame( 'C', $generator->createChecksum( 'QaQa' ) );
58
-		$this->assertSame( 'C', $generator->createChecksum( 'qaqa' ) );
57
+		$this->assertSame('C', $generator->createChecksum('QaQa'));
58
+		$this->assertSame('C', $generator->createChecksum('qaqa'));
59 59
 
60
-		$this->assertSame( 'D', $generator->createChecksum( 'xxxx' ) );
61
-		$this->assertSame( 'D', $generator->createChecksum( 'XXXX' ) );
60
+		$this->assertSame('D', $generator->createChecksum('xxxx'));
61
+		$this->assertSame('D', $generator->createChecksum('XXXX'));
62 62
 	}
63 63
 
64 64
 	public function testChecksumIsOneOfTheExpectedCharacters(): void {
65
-		$characters = [ 'A', 'B', 'C' ];
66
-		$generator = new ChecksumGenerator( $characters );
65
+		$characters = ['A', 'B', 'C'];
66
+		$generator = new ChecksumGenerator($characters);
67 67
 
68 68
 		//$distribution = ['A' => 0, 'B' => 0, 'C' => 0];
69 69
 
70
-		foreach ( $this->getRandomStrings() as $string ) {
70
+		foreach ($this->getRandomStrings() as $string) {
71 71
 			//$distribution[$generator->createChecksum( $string )]++;
72 72
 			$this->assertContains(
73
-				$generator->createChecksum( $string ),
73
+				$generator->createChecksum($string),
74 74
 				$characters
75 75
 			);
76 76
 		}
@@ -79,17 +79,17 @@  discard block
 block discarded – undo
79 79
 	}
80 80
 
81 81
 	public function getRandomStrings(): iterable {
82
-		$characters = str_split( 'ACDEFKLMNPRSTWXYZ349-' );
83
-		$characterCount = count( $characters );
82
+		$characters = str_split('ACDEFKLMNPRSTWXYZ349-');
83
+		$characterCount = count($characters);
84 84
 
85
-		for ( $i = 0; $i < 1000; $i++ ) {
85
+		for ($i = 0; $i < 1000; $i++) {
86 86
 			yield implode(
87 87
 				'',
88 88
 				array_map(
89
-					function() use ( $characters, $characterCount ) {
90
-						return $characters[mt_rand( 0, $characterCount - 1 )];
89
+					function() use ($characters, $characterCount) {
90
+						return $characters[mt_rand(0, $characterCount - 1)];
91 91
 					},
92
-					array_fill( 0, 10, null )
92
+					array_fill(0, 10, null)
93 93
 				)
94 94
 			);
95 95
 		}
Please login to merge, or discard this patch.