Analysis result

Scanned at : 2021-11-03
Analyzed by : Phan

Your PHP Code Snippet

                    
<?php
class Tehsaurux {

    public function __construct()
    {
        $this->tsx = $this->getTsx();
        $this->syn_src_limit = 10;
        $this->mode = 'strict';
    }

    public function find($keyword, $mode = null)
    {
        if ($mode !== null) {
            $this->setMode($mode);
        }

        if (isset($this->tsx[$keyword])) {
            // grab a random synonym
            $syns = $this->tsx[$keyword][$mode];
            $len = count($syns);
            $idx = rand(1, $len) - 1;
            return $syns[$idx];

        } else {
            // keyword not defined in tsx
            $this->makeDefinition($keyword);
            return $this->find($keyword, $mode);
        }
    }

    public function getTsx()
    {
        $path = __DIR__.'\tehsaurux.json';

        if (!file_exists($path)) {
            file_put_contents($path, json_encode(''));
        }

        $contents = file_get_contents($path);

        return json_decode($contents, true);
    }

    public function setMode($mode) {
        if ($mode == 'l' | $mode == 'loose') {
            $this->mode = 'loose';
            return;
        }

        if ($mode == 's' | $mode == 'strict') {
            $this->mode = 'strict';
            return;
        }

        if ($mode == 'f' | $mode == 'favorites') {
            $this->mode = 'favorites';
        }
    }

    private function saveEntry(string $keyword = null, array $entries = null)
    {
        if ($entries !== null) {
            if (array_key_exists('loose', $entries)) {
                $this->tsx[$keyword]['loose'] = $entries['loose'];
            }

            if (array_key_exists('strict', $entries)) {
                $this->tsx[$keyword]['strict'] = $entries['strict'];
            }

            if (array_key_exists('favorites', $entries)) {
                $this->tsx[$keyword]['favorites'] = $entries['favorites'];
            }
        }

        $this->save();
    }

    private function makeDefinition($keyword)
    {
        $this->makeSources($keyword);
        $entry = $this->fetchWords($keyword);
        $this->saveEntry($keyword, $entry);
    }

    private function fetchWords($keyword)
    {
        $loose = [];
        $strict = [];

        $sources = ['thesaurus', 'collins', 'oxford'];

        foreach ($sources as $source) {
            $filename = "\\" . $source . '.txt';
            if (file_exists(__DIR__ . $filename)) {
                $contents = file_get_contents(__DIR__.$filename);
                file_put_contents(__DIR__.$filename, '');
                $synonyms = explode(',', $contents);

                $strict[] = $synonyms;
                foreach ($synonyms as $idx => $syn) {
                    $loose[] = $syn;
                    if ($idx == $this->syn_src_limit) {
                        break;
                    }
                }
            }
        }

        $loose = array_unique($loose);

        $stricter = call_user_func_array('array_intersect', $strict);

        $loose = array_values(array_filter($loose));
        $strict = array_values(array_filter($stricter));

        if (count($strict) < 1) {
            $strict = [$keyword];
        }

        return ['loose' => $loose, 'strict' => $strict];
    }

    private function save()
    {
        try {
            file_put_contents(__DIR__.'\tehsaurux.json', json_encode($this->tsx));
        } catch (Exception $e) {
            echo "Caught an exception trying to save. It says ", $e->getMessage();
        }
    }

    public function makeSources($keyword)
    {
        try {
            $cmd = escapeshellcmd('scrapy crawl tehsaurux -a keyword=' . $keyword);
            system($cmd);
        } catch (Exception $e) {
            echo "Had a hard time getting the words. Here's the error message: " . $e->getMessage();
      }

    }
}

?>
                    
                

Like you the result ?

Good jobs, the log is clean!