Issues (15)

examples/bst/addressBookSearch.php (1 issue)

Labels
Severity
1
#!/usr/bin/php
2
<?php
3
/*
4
 * Forest: binarySearchTree.php
5
 * User: Sebastian Böttger <[email protected]>
6
 * created at 24.12.19, 14:53
7
 */
8
require_once "../../vendor/autoload.php";
9
require_once "AddressBookBinaryTree.php";
10
require_once "AddressBookAVLTree.php";
11
require_once "AddressBookEntry.php";
12
13
function generatorPhoneEntries()
14
{
15
    $handle = fopen("entries.csv", "r+");
16
    while (($item = fgetcsv($handle, 1000, ";")) !== false) {
0 ignored issues
show
It seems like $handle can also be of type false; however, parameter $handle of fgetcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

16
    while (($item = fgetcsv(/** @scrutinizer ignore-type */ $handle, 1000, ";")) !== false) {
Loading history...
17
        yield new AddressBookEntry($item[1], $item[0], $item[2], trim($item[3]));
18
    }
19
}
20
21
if ($argc < 4) {
22
    echo "Please run:\nphp addressBookSearch.php LastName FirstName (Binary|AVL)\n";
23
    exit();
24
}
25
26
if ($argv[3] === "AVL") {
27
    $dataStructure = "AVLTree";
28
    $addressBook = new AddressBookAvlTree();
29
} else if ($argv[3] === "Binary") {
30
    $dataStructure = "BinaryTree";
31
    $addressBook = new AddressBookBinaryTree();
32
} else {
33
    echo "Invalid data structure. Use either 'Binary' or 'AVL'!\n";
34
}
35
36
foreach (generatorPhoneEntries() as $addressBookEntry) {
37
    $addressBook->insert($addressBookEntry);
38
}
39
40
$timeBefore = microtime();
41
$result = $addressBook->search(new AddressBookEntry($argv[1], $argv[2]));
42
$timeAfter = microtime();
43
44
$time = $timeAfter - $timeBefore; // runtime of search
45
46
if (null === $result) {
47
    echo "No entry found\n";
48
} else {
49
    $searchResultItem = $result->getItem();
50
    echo $searchResultItem->getLastName() . ", " . $searchResultItem->getFirstName() . "\n";
51
    echo $searchResultItem->getAddress() . "\n";
52
    echo "Phone No.: " . $searchResultItem->getPhoneNumber() . "\n";
53
}
54
echo "Time to search with $dataStructure: " . sprintf("%1.2E", $time) . "\n";
55