Skip to content

Commit 5136943

Browse files
davidszkibamona-shakiba
authored andcommitted
GH-791 Continue refactoring
1 parent d3fd88f commit 5136943

33 files changed

+215
-225
lines changed

classes/local/none.php renamed to classes/local/fail.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,49 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* Сlass status.
18+
* Сlass fail.
1919
*
2020
* @package local_catquiz
21-
* @copyright 2023 Wunderbyte GmbH <[email protected]>
21+
* @copyright 2025 Wunderbyte GmbH <[email protected]>
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

2525
namespace local_catquiz\local;
2626

2727
use Exception;
28-
use local_catquiz\local\status;
2928

3029
/**
31-
* Provides methods to obtain results.
32-
*
30+
* Overrides abstract methods from result
3331
*
3432
* @package local_catquiz
35-
* @copyright 2023 Wunderbyte GmbH <[email protected]>
33+
* @copyright 2025 Wunderbyte GmbH <[email protected]>
3634
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3735
*/
38-
class none extends result {
36+
class fail extends result {
37+
/**
38+
* Just returns the current result
39+
*
40+
* @return result
41+
*/
3942
public function and_then(callable $op): result {
4043
return $this;
4144
}
4245

46+
/**
47+
* Calls the given callable
48+
*
49+
* @return result
50+
*/
4351
public function or_else(callable $op): result {
4452
return $op($this);
4553
}
4654

47-
public function expect() {
55+
/**
56+
* Throws an exception
57+
*
58+
* @throws Exception
59+
*/
60+
public function expect(): result {
4861
throw new Exception($this->geterrormessage());
4962
}
5063
}

classes/local/result.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* Сlass status.
18+
* Сlass result.
1919
*
2020
* @package local_catquiz
21-
* @copyright 2023 Wunderbyte GmbH <[email protected]>
21+
* @copyright 2025 Wunderbyte GmbH <[email protected]>
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

@@ -30,9 +30,8 @@
3030
/**
3131
* Provides methods to obtain results.
3232
*
33-
*
3433
* @package local_catquiz
35-
* @copyright 2023 Wunderbyte GmbH <[email protected]>
34+
* @copyright 2025 Wunderbyte GmbH <[email protected]>
3635
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3736
*/
3837
abstract class result {
@@ -63,33 +62,43 @@ public function __construct($value = null, string $status = status::OK) {
6362
* @param string $status
6463
* @param mixed|null $value
6564
*
66-
* @return none
67-
*
65+
* @return fail
6866
*/
69-
public static function err(string $status = status::ERROR_GENERAL, $value = null) {
70-
return new none($value, $status);
67+
public static function err(string $status = status::ERROR_GENERAL, $value = null): fail {
68+
return new fail($value, $status);
7169
}
7270

7371
/**
7472
* Returns OK result.
7573
*
7674
* @param mixed|null $value
7775
*
78-
* @return some
79-
*
76+
* @return success
8077
*/
81-
public static function ok($value = null) {
82-
return new some($value, status::OK);
78+
public static function ok($value = null): success {
79+
return new success($value, status::OK);
8380
}
8481

82+
/**
83+
* Calls the given callable if successful
84+
*
85+
* @return result
86+
*/
8587
abstract public function and_then(callable $op): result;
8688

89+
/**
90+
* Calls the given callable if not successful
91+
*
92+
* @return result
93+
*/
8794
abstract public function or_else(callable $op): result;
8895

8996
/**
97+
* Throws an exception if not successful
98+
*
9099
* @throws Exception
91100
*/
92-
abstract public function expect();
101+
abstract public function expect(): result;
93102

94103
/**
95104
* Returns status.

classes/local/some.php renamed to classes/local/success.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,45 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* Сlass status.
18+
* Сlass success.
1919
*
2020
* @package local_catquiz
21-
* @copyright 2023 Wunderbyte GmbH <[email protected]>
21+
* @copyright 2025 Wunderbyte GmbH <[email protected]>
2222
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2323
*/
2424

2525
namespace local_catquiz\local;
2626

27-
use local_catquiz\local\status;
28-
2927
/**
30-
* Provides methods to obtain results.
31-
*
28+
* Overrides abstract methods from result
3229
*
3330
* @package local_catquiz
34-
* @copyright 2023 Wunderbyte GmbH <[email protected]>
31+
* @copyright 2025 Wunderbyte GmbH <[email protected]>
3532
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3633
*/
37-
class some extends result {
38-
34+
class success extends result {
35+
/**
36+
* Calls the given callable
37+
*
38+
* @return result
39+
*/
3940
public function and_then(callable $op): result {
4041
return $op($this);
4142
}
4243

44+
/**
45+
* Just returns the current result
46+
*
47+
* @return result
48+
*/
4349
public function or_else(callable $op): result {
4450
return $this;
4551
}
4652

47-
public function expect() {
53+
/**
54+
* Just returns the current result
55+
*/
56+
public function expect(): result {
4857
return $this;
4958
}
5059
}

classes/teststrategy/preselect_task.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function process(array &$context, callable $nexttask): result {
8989
* @param callable $next Callable that calls the next middleware
9090
* @return result
9191
*/
92-
abstract public function run(array &$context, callable $next): result;
92+
abstract public function run(array &$context): result;
9393

9494
/**
9595
* If a middleware requires a specific key to be available in the $context

classes/teststrategy/preselect_task/addscalestandarderror.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ class addscalestandarderror extends preselect_task {
5858
* @return result
5959
*
6060
*/
61-
public function run(array &$context, callable $next): result {
61+
public function run(array &$context): result {
6262
$this->progress = $context['progress'];
6363
$responses = $this->progress->get_user_responses();
6464
if (! $responses) {
65-
return $next($context);
65+
return result::ok($context);
6666
}
6767

6868
$userresponses = model_responses::create_from_array([$context['userid'] => ['component' => $responses]]);
@@ -72,7 +72,7 @@ public function run(array &$context, callable $next): result {
7272
$context['se'][$catscaleid] = $se;
7373
}
7474

75-
return $next($context);
75+
return result::ok($context);
7676
}
7777

7878
/**

classes/teststrategy/preselect_task/checkpagereload.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3737
*/
3838
final class checkpagereload extends preselect_task {
39-
4039
/**
4140
* @var progress $progress
4241
*/
@@ -51,13 +50,14 @@ final class checkpagereload extends preselect_task {
5150
* @return result
5251
*
5352
*/
54-
public function run(array &$context, callable $next): result {
53+
public function run(array &$context): result {
5554
$this->progress = $context['progress'];
56-
if (($this->progress->is_first_question() && !$this->progress->get_last_question())
55+
if (
56+
($this->progress->is_first_question() && !$this->progress->get_last_question())
5757
|| $this->progress->has_new_response()
5858
|| $this->progress->get_force_new_question()
5959
) {
60-
return $next($context);
60+
return result::ok($context);
6161
}
6262

6363
return result::ok($this->progress->get_last_question());

classes/teststrategy/preselect_task/filterbyquestionsperscale.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ class filterbyquestionsperscale extends preselect_task {
5858
* @return result
5959
*
6060
*/
61-
public function run(array &$context, callable $next): result {
61+
public function run(array &$context): result {
6262
$this->progress = $context['progress'];
6363

6464
if (!in_array($context['teststrategy'], [LOCAL_CATQUIZ_STRATEGY_ALLSUBS])) {
65-
return $next($context);
65+
return result::ok($context);
6666
}
6767

6868
$minquestionsperscale = $context['min_attempts_per_scale'];
@@ -88,7 +88,7 @@ public function run(array &$context, callable $next): result {
8888
PHP_EOL
8989
);
9090
}
91-
return $next($context);
91+
return result::ok($context);
9292
}
9393

9494
// If we are here, not all scales have the minimum questions.
@@ -104,7 +104,7 @@ public function run(array &$context, callable $next): result {
104104
);
105105
}
106106
}
107-
return $next($context);
107+
return result::ok($context);
108108
}
109109

110110
/**

classes/teststrategy/preselect_task/filterbystandarderror.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,11 @@
4444
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4545
*/
4646
class filterbystandarderror extends preselect_task {
47-
4847
/**
4948
* @var progress
5049
*/
5150
private progress $progress;
5251

53-
/**
54-
* @var callable $next
55-
*/
56-
private $next;
57-
5852
/**
5953
* Run method.
6054
*
@@ -64,21 +58,20 @@ class filterbystandarderror extends preselect_task {
6458
* @return result
6559
*
6660
*/
67-
public function run(array &$context, callable $next): result {
61+
public function run(array &$context): result {
6862
$this->context = $context;
69-
$this->next = $next;
7063
$this->progress = $context['progress'];
7164

7265
if ($this->progress->is_first_question()) {
73-
return $next($context);
66+
return result::ok($context);
7467
}
7568

7669
if (!$this->progress->has_new_response()) {
77-
return $next($context);
70+
return result::ok($context);
7871
}
7972

8073
if ($this->progress->get_last_question()->is_pilot) {
81-
return $next($context);
74+
return result::ok($context);
8275
}
8376

8477
$lastquestion = $this->progress->get_last_question();
@@ -98,7 +91,8 @@ public function run(array &$context, callable $next): result {
9891
getenv('CATQUIZ_CREATE_TESTOUTPUT') && printf(
9992
"%d: [SE] drop %s%s",
10093
count($this->progress->get_playedquestions()),
101-
(catscale::return_catscale_object($scaleid))->name, PHP_EOL
94+
(catscale::return_catscale_object($scaleid))->name,
95+
PHP_EOL
10296
);
10397
$this->progress->drop_scale($scaleid);
10498
$inheritscales = $this->get_scale_heirs($scaleid);
@@ -144,7 +138,7 @@ public function run(array &$context, callable $next): result {
144138
}
145139
}
146140

147-
return $next($context);
141+
return result::ok($context);
148142
}
149143

150144
/**
@@ -210,7 +204,7 @@ private function filter_for_cat(array $updatedscales): result {
210204
if ($drop) {
211205
return result::err(status::ERROR_NO_REMAINING_QUESTIONS);
212206
}
213-
return ($this->next)($this->context);
207+
return result::ok($this->context);
214208
}
215209

216210
/**

classes/teststrategy/preselect_task/filterbytestinfo.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4141
*/
4242
class filterbytestinfo extends preselect_task {
43-
4443
/**
4544
* @var progress
4645
*/
@@ -55,17 +54,18 @@ class filterbytestinfo extends preselect_task {
5554
* @return result
5655
*
5756
*/
58-
public function run(array &$context, callable $next): result {
57+
public function run(array &$context): result {
5958
$this->context = $context;
6059
$this->progress = $context['progress'];
6160

62-
if (!in_array($context['teststrategy'], [ // TODO: use something like strategy::supports_dynamic_scales()!
61+
if (!in_array($context['teststrategy'], [
6362
LOCAL_CATQUIZ_STRATEGY_LOWESTSUB,
6463
LOCAL_CATQUIZ_STRATEGY_HIGHESTSUB,
6564
LOCAL_CATQUIZ_STRATEGY_RELSUBS,
6665
LOCAL_CATQUIZ_STRATEGY_ALLSUBS,
67-
])) {
68-
return $next($context);
66+
])
67+
) {
68+
return result::ok($context);
6969
}
7070

7171
foreach ($this->progress->get_abilities() as $scaleid => $ability) {
@@ -146,7 +146,7 @@ public function run(array &$context, callable $next): result {
146146
}
147147
}
148148

149-
return $next($context);
149+
return result::ok($context);
150150
}
151151

152152
/**

0 commit comments

Comments
 (0)