Skip to content

Commit c189977

Browse files
committed
Convert newinstance to anonymous classes
1 parent 7349bc1 commit c189977

File tree

5 files changed

+54
-56
lines changed

5 files changed

+54
-56
lines changed

src/main/php/rdbms/mysqlx/MySqlPassword.class.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
<?php namespace rdbms\mysqlx;
22

3+
use lang\Enum;
4+
use math\{BigInt, BigFloat};
5+
36
/**
47
* MySQL password functions
58
*
69
* @see php://sha1
710
* @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol
811
* @test xp://net.xp_framework.unittest.rdbms.mysql.MySqlPasswordTest
912
*/
10-
abstract class MySqlPassword extends \lang\Enum {
11-
public static
12-
$PROTOCOL_40= null,
13-
$PROTOCOL_41= null;
13+
abstract class MySqlPassword extends Enum {
14+
public static $PROTOCOL_40, $PROTOCOL_41;
1415

1516
static function __static() {
16-
self::$PROTOCOL_40= newinstance(__CLASS__, [0, 'PROTOCOL_40'], '{
17+
self::$PROTOCOL_40= new class(0, 'PROTOCOL_40') extends MySqlPassword {
1718
static function __static() { }
1819

1920
public static function hash($in) {
20-
$nr= new \math\BigInt(1345345333);
21-
$nr2= new \math\BigInt(0x12345671);
22-
$add= new \math\BigInt(7);
21+
$nr= new BigInt(1345345333);
22+
$nr2= new BigInt(0x12345671);
23+
$add= new BigInt(7);
2324

2425
for ($i= 0, $s= strlen($in); $i < $s; $i++) {
2526
$ord= ord($in[$i]);
@@ -29,42 +30,43 @@ public static function hash($in) {
2930
$nr2= $nr2->multiply0(0x100)->bitwiseXor($nr)->add0($nr2);
3031
$add= $add->add0($ord);
3132
}
32-
return array($nr->bitwiseAnd(0x7FFFFFFF), $nr2->bitwiseAnd(0x7FFFFFFF));
33+
return [$nr->bitwiseAnd(0x7FFFFFFF), $nr2->bitwiseAnd(0x7FFFFFFF)];
3334
}
3435

3536
public function scramble($password, $message) {
36-
if ("" === $password || null === $password) return "";
37+
if ('' === $password || null === $password) return '';
3738

3839
$hp= self::hash($password);
3940
$hm= self::hash($message);
4041
$SEED_MAX= 0x3FFFFFFF;
4142

4243
$seed1= $hp[0]->bitwiseXor($hm[0])->modulo($SEED_MAX);
4344
$seed2= $hp[1]->bitwiseXor($hm[1])->modulo($SEED_MAX);
44-
$to= "";
45+
$to= '';
4546
for ($i= 0, $s= strlen($message); $i < $s; $i++) {
4647
$seed1= $seed1->multiply0(3)->add0($seed2)->modulo($SEED_MAX);
4748
$seed2= $seed1->add0($seed2)->add0(33)->modulo($SEED_MAX);
48-
$div= new \math\BigFloat(bcdiv((string)$seed1, $SEED_MAX, 14)); // Explicitely pass precision, HHVM bug
49+
$div= new BigFloat(bcdiv((string)$seed1, $SEED_MAX));
4950
$to.= chr($div->multiply(31)->intValue() + 64);
5051
}
5152
$seed1= $seed1->multiply0(3)->add0($seed2)->modulo($SEED_MAX);
5253
$seed2= $seed1->add0($seed2)->add0(33)->modulo($SEED_MAX);
5354

54-
$div= new \math\BigFloat(bcdiv((string)$seed1, $SEED_MAX, 14)); // Explicitely pass precision, HHVM bug
55+
$div= new BigFloat(bcdiv((string)$seed1, $SEED_MAX));
5556
$result= $to ^ str_repeat(chr($div->multiply(31)->intValue()), strlen($message));
5657
return $result;
5758
}
58-
}');
59-
self::$PROTOCOL_41= newinstance(__CLASS__, [1, 'PROTOCOL_41'], '{
59+
};
60+
self::$PROTOCOL_41= new class(1, 'PROTOCOL_41') extends MySqlPassword {
6061
static function __static() { }
62+
6163
public function scramble($password, $message) {
62-
if ("" === $password || null === $password) return "";
64+
if ('' === $password || null === $password) return '';
6365

6466
$stage1= sha1($password, true);
6567
return sha1($message.sha1($stage1, true), true) ^ $stage1;
6668
}
67-
}');
69+
};
6870
}
6971

7072
/**

src/test/php/rdbms/unittest/DataSetTest.class.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php namespace rdbms\unittest;
22

33
use lang\IllegalArgumentException;
4-
use rdbms\{Column, DriverManager, Peer, ResultIterator, SQLException, Statement};
54
use rdbms\unittest\dataset\Job;
65
use rdbms\unittest\mock\MockResultSet;
6+
use rdbms\{Column, DBEvent, DriverManager, Peer, ResultIterator, SQLException, Statement};
77
use unittest\TestCase;
8-
use util\{Date, DateUtil, NoSuchElementException};
98
use util\log\BoundLogObserver;
9+
use util\{Date, DateUtil, NoSuchElementException};
1010

1111
/**
1212
* O/R-mapping API unit test
@@ -472,15 +472,15 @@ public function doDelete() {
472472

473473
#[@test]
474474
public function percentSign() {
475-
$observer= $this->getConnection()->addObserver(newinstance(BoundLogObserver::class, [], '{
475+
$observer= $this->getConnection()->addObserver(new class() implements BoundLogObserver {
476476
public $statements= [];
477477
public static function instanceFor($arg) { /* NOOP */ }
478478
public function update($observable, $event= null) {
479-
if ($event instanceof \\rdbms\\DBEvent && "query" == $event->getName()) {
479+
if ($event instanceof DBEvent && 'query' === $event->getName()) {
480480
$this->statements[]= $event->getArgument();
481481
}
482482
}
483-
}'));
483+
});
484484
$j= new Job();
485485
$j->setTitle('Percent%20Sign');
486486
$j->insert();

src/test/php/rdbms/unittest/JoinIteratorTest.class.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php namespace rdbms\unittest;
22

3-
use rdbms\{CachedResults, Criteria, DSN};
43
use rdbms\join\{JoinIterator, JoinProcessor};
54
use rdbms\mysql\MySQLConnection;
65
use rdbms\unittest\dataset\{Job, Person};
76
use rdbms\unittest\mock\MockResultSet;
7+
use rdbms\{CachedResults, ConnectionManager, Criteria, DSN};
88
use unittest\TestCase;
9-
use util\NoSuchElementException;
9+
use util\{Date, NoSuchElementException};
1010

1111
/**
1212
* Test JoinProcessor class
@@ -19,12 +19,9 @@
1919
*/
2020
class JoinIteratorTest extends TestCase {
2121

22-
/**
23-
* Setup test
24-
*/
2522
#[@beforeClass]
2623
public static function registerConnection() {
27-
\rdbms\ConnectionManager::getInstance()->register(new MySQLConnection(new DSN('mysql://localhost:3306/')), 'jobs');
24+
ConnectionManager::getInstance()->register(new MySQLConnection(new DSN('mysql://localhost:3306/')), 'jobs');
2825
}
2926

3027
#[@test, @expect(NoSuchElementException::class)]
@@ -44,7 +41,7 @@ public function resultHasNextTest() {
4441
[
4542
JoinProcessor::FIRST.'_job_id' => '11',
4643
JoinProcessor::FIRST.'_title' => 'clean toilette',
47-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
44+
JoinProcessor::FIRST.'_valid_from' => new Date(),
4845
JoinProcessor::FIRST.'_expire_at' => '',
4946
't1_person_id' => '11',
5047
't1_name' => 'Schultz',
@@ -54,7 +51,7 @@ public function resultHasNextTest() {
5451
[
5552
JoinProcessor::FIRST.'_job_id' => '11',
5653
JoinProcessor::FIRST.'_title' => 'clean toilette',
57-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
54+
JoinProcessor::FIRST.'_valid_from' => new Date(),
5855
JoinProcessor::FIRST.'_expire_at' => '',
5956
't1_person_id' => '12',
6057
't1_name' => 'Friebe',
@@ -76,25 +73,25 @@ public function multipleResultTest() {
7673
[
7774
JoinProcessor::FIRST.'_job_id' => '11',
7875
JoinProcessor::FIRST.'_title' => 'clean toilette',
79-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
76+
JoinProcessor::FIRST.'_valid_from' => new Date(),
8077
JoinProcessor::FIRST.'_expire_at' => '',
8178
],
8279
[
8380
JoinProcessor::FIRST.'_job_id' => '11',
8481
JoinProcessor::FIRST.'_title' => 'clean toilette',
85-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
82+
JoinProcessor::FIRST.'_valid_from' => new Date(),
8683
JoinProcessor::FIRST.'_expire_at' => '',
8784
],
8885
[
8986
JoinProcessor::FIRST.'_job_id' => '12',
9087
JoinProcessor::FIRST.'_title' => 'second job',
91-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
88+
JoinProcessor::FIRST.'_valid_from' => new Date(),
9289
JoinProcessor::FIRST.'_expire_at' => '',
9390
],
9491
[
9592
JoinProcessor::FIRST.'_job_id' => '13',
9693
JoinProcessor::FIRST.'_title' => 'third job',
97-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
94+
JoinProcessor::FIRST.'_valid_from' => new Date(),
9895
JoinProcessor::FIRST.'_expire_at' => '',
9996
],
10097
]
@@ -117,7 +114,7 @@ public function multipleJoinResultTest() {
117114
[
118115
JoinProcessor::FIRST.'_job_id' => '11',
119116
JoinProcessor::FIRST.'_title' => 'clean toilette',
120-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
117+
JoinProcessor::FIRST.'_valid_from' => new Date(),
121118
JoinProcessor::FIRST.'_expire_at' => '',
122119
JoinProcessor::pathToKey(['PersonJob']).'_person_id' => '11',
123120
JoinProcessor::pathToKey(['PersonJob']).'_name' => 'Schultz',
@@ -127,7 +124,7 @@ public function multipleJoinResultTest() {
127124
[
128125
JoinProcessor::FIRST.'_job_id' => '11',
129126
JoinProcessor::FIRST.'_title' => 'clean toilette',
130-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
127+
JoinProcessor::FIRST.'_valid_from' => new Date(),
131128
JoinProcessor::FIRST.'_expire_at' => '',
132129
JoinProcessor::pathToKey(['PersonJob']).'_person_id' => '12',
133130
JoinProcessor::pathToKey(['PersonJob']).'_name' => 'Müller',
@@ -137,7 +134,7 @@ public function multipleJoinResultTest() {
137134
[
138135
JoinProcessor::FIRST.'_job_id' => '12',
139136
JoinProcessor::FIRST.'_title' => 'second job',
140-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
137+
JoinProcessor::FIRST.'_valid_from' => new Date(),
141138
JoinProcessor::FIRST.'_expire_at' => '',
142139
JoinProcessor::pathToKey(['PersonJob']).'_person_id' => '11',
143140
JoinProcessor::pathToKey(['PersonJob']).'_name' => 'Schultz',
@@ -147,7 +144,7 @@ public function multipleJoinResultTest() {
147144
[
148145
JoinProcessor::FIRST.'_job_id' => '13',
149146
JoinProcessor::FIRST.'_title' => 'third job',
150-
JoinProcessor::FIRST.'_valid_from' => new \util\Date(),
147+
JoinProcessor::FIRST.'_valid_from' => new Date(),
151148
JoinProcessor::FIRST.'_expire_at' => '',
152149
JoinProcessor::pathToKey(['PersonJob']).'_person_id' => null,
153150
JoinProcessor::pathToKey(['PersonJob']).'_name' => null,

src/test/php/rdbms/unittest/integration/RdbmsIntegrationTest.class.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -626,18 +626,17 @@ public function selectSmallintZero() {
626626

627627
#[@test]
628628
public function observe() {
629-
$observer= newinstance(Observer::class, [], [
630-
'observations' => [],
631-
'numberOfObservations' => function() {
632-
return sizeof($this->observations);
633-
},
634-
'observationAt' => function($i) {
635-
return $this->observations[$i]['arg'];
636-
},
637-
'update' => function($obs, $arg= null) {
629+
$observer= new class() implements Observer {
630+
public $observations= [];
631+
632+
public function numberOfObservations() { return sizeof($this->observations); }
633+
634+
public function observationAt($i) { return $this->observations[$i]['arg']; }
635+
636+
public function update($obs, $arg= null) {
638637
$this->observations[]= ['observable' => $obs, 'arg' => $arg];
639638
}
640-
]);
639+
};
641640

642641
$db= $this->db();
643642
$db->addObserver($observer);

src/test/php/rdbms/unittest/tds/FreeTdsConfigLocationTest.class.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class FreeTdsConfigLocationTest extends \unittest\TestCase {
1313

1414
#[@test]
1515
public function noAlternativesFound() {
16-
$fixture= newinstance(FreeTdsLookup::class, [], [
17-
'parse' => function() { throw new IllegalStateException('Should never be called!'); },
18-
'locateConf' => function() { return null; }
19-
]);
16+
$fixture= new class() extends FreeTdsLookup {
17+
public function parse() { throw new IllegalStateException('Should never be called!'); }
18+
public function locateConf() { return null; }
19+
};
2020

2121
$dsn= new DSN('sybase://test');
2222
$fixture->lookup($dsn);
@@ -25,10 +25,10 @@ public function noAlternativesFound() {
2525

2626
#[@test]
2727
public function fileReturned() {
28-
$fixture= newinstance(FreeTdsLookup::class, [], [
29-
'parse' => function() { return ['test' => ['host' => $this->conf->getFilename(), 'port' => 1999]]; },
30-
'locateConf' => function() { return new File('it.worked'); }
31-
]);
28+
$fixture= new class() extends FreeTdsLookup {
29+
public function parse() { return ['test' => ['host' => $this->conf->getFilename(), 'port' => 1999]]; }
30+
public function locateConf() { return new File('it.worked'); }
31+
};
3232

3333
$dsn= new DSN('sybase://test');
3434
$fixture->lookup($dsn);

0 commit comments

Comments
 (0)