Issues (3)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/engine/DecisionNode.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace lucidtaz\minimax\engine;
6
7
use lucidtaz\minimax\game\GameState;
8
use lucidtaz\minimax\game\Player;
9
10
/**
11
 * Node in the decision search tree
12
 *
13
 * An object of this class can be queried for its ideal decision (and according
14
 * score) by calling the decide() method. It will recursively construct child
15
 * nodes and evaluate them using that method as well.
16
 */
17
class DecisionNode
18
{
19
    /**
20
     * @var Player The player to optimize for.
21
     */
22
    private $objectivePlayer;
23
24
    /**
25
     * @var GameState The current GameState to base future decisions on.
26
     */
27
    private $state;
28
29
    /**
30
     * @var int Limit on how deep we can continue to search, recursion limiter.
31
     */
32
    private $depthLeft;
33
34
    /**
35
     * @var NodeType Whether we are a min-node or a max-node. This enables the
36
     * caller to select either the most favorable or the least favorable
37
     * outcome.
38
     */
39
    private $type;
40
41
    /**
42
     * @var AlphaBeta Constraints for alpha-beta pruning
43
     */
44
    private $alphaBeta;
45
46
    /**
47
     * @param Player $objectivePlayer The Player to optimize for
48
     * @param GameState $state Current GameState to base decisions on
49
     * @param int $depthLeft Recursion limiter
50
     * @param NodeType $type Signifies whether to minimize or maximize the score
51
     * @param AlphaBeta $alphaBeta Range of potential scores to check
52
     */
53 14
    public function __construct(Player $objectivePlayer, GameState $state, int $depthLeft, NodeType $type, AlphaBeta $alphaBeta)
54
    {
55 14
        $this->objectivePlayer = $objectivePlayer;
56 14
        $this->state = $state;
57 14
        $this->depthLeft = $depthLeft;
58 14
        $this->type = $type;
59 14
        $this->alphaBeta = $alphaBeta;
60 14
    }
61
62
    /**
63
     * Determine the ideal move for this node
64
     *
65
     * This means either the best or the worst possible outcome for the
66
     * objective player, based on who is actually playing. (If the objective
67
     * player is currently playing, we take the best outcome, otherwise we take
68
     * the worst. This reflects that the opponent also plays optimally.)
69
     */
70 14
    public function traverseGameTree(): TraversalResult
71
    {
72 14
        if ($this->depthLeft === 0) {
73 13
            return TraversalResult::withoutMove($this->makeLeafEvaluation(), Analytics::forLeafNode());
74
        }
75
76 13
        $possibleMoves = $this->state->getPossibleMoves();
77 13
        if (empty($possibleMoves)) {
78 9
            return TraversalResult::withoutMove($this->makeLeafEvaluation(), Analytics::forLeafNode());
79
        }
80
81 13
        $analytics = Analytics::forInternalNode();
82 13
        $idealMove = null;
83 13
        $idealMoveResult = null;
84 13
        foreach ($possibleMoves as $move) {
85 13
            if (!$this->alphaBeta->isPositiveRange()) {
86
                // Subtree became fruitless, return to caller asap
87 12
                break;
88
            }
89
90 13
            $moveResult = $this->getChildResult($move);
91 13
            $analytics->add($moveResult->analytics);
92 13
            $this->alphaBeta->update($moveResult->evaluation, $this->type);
93 13
            if ($idealMoveResult === null || $this->isIdealOver($moveResult->evaluation, $idealMoveResult->evaluation)) {
94 13
                $idealMove = $move;
95 13
                $idealMoveResult = $moveResult;
96
            }
97
        }
98
99 13
        return TraversalResult::create($idealMove, $idealMoveResult->evaluation, $analytics);
0 ignored issues
show
It seems like $idealMove defined by null on line 82 can be null; however, lucidtaz\minimax\engine\TraversalResult::create() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
100
    }
101
102
    /**
103
     * Formulate the evaluation, this node being a leaf node
104
     */
105 14
    private function makeLeafEvaluation(): Evaluation
106
    {
107 14
        $result = new Evaluation();
108 14
        $result->age = $this->depthLeft;
109 14
        $result->score = $this->state->evaluateScore($this->objectivePlayer);
110 14
        return $result;
111
    }
112
113
    /**
114
     * Recursively evaluate a child decision
115
     *
116
     * Apply a move and evaluate the outcome
117
     *
118
     * @param GameState $stateAfterMove The GameState that was created as a
119
     * result of a possible move.
120
     */
121 13
    private function getChildResult(GameState $stateAfterMove): TraversalResult
122
    {
123 13
        $nextPlayerIsFriendly = $stateAfterMove->getNextPlayer()->isFriendsWith($this->objectivePlayer);
124 13
        $nextDecisionPoint = new static(
125 13
            $this->objectivePlayer,
126 13
            $stateAfterMove,
127 13
            $this->depthLeft - 1,
128 13
            $nextPlayerIsFriendly ? NodeType::MAX() : NodeType::MIN(),
129 13
            clone $this->alphaBeta
130
        );
131 13
        return $nextDecisionPoint->traverseGameTree();
132
    }
133
134
    /**
135
     * Compare two evaluations
136
     *
137
     * The meaning of "best" is decided by the "ideal" member variable
138
     * comparator
139
     */
140 12
    private function isIdealOver(Evaluation $a, Evaluation $b): bool
141
    {
142 12
        $ideal = $this->type == NodeType::MIN()
143 11
            ? Evaluation::getWorstComparator()
144 12
            : Evaluation::getBestComparator();
145 12
        $idealEvaluationResult = $ideal($a, $b);
146 12
        return $idealEvaluationResult > 0;
147
    }
148
}
149