<commit_before><?php declare(strict_types=1);


namespace AlgoWeb\PODataLaravel\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Mockery\Mock;
use POData\Common\InvalidOperationException;
use ReflectionException;
use ReflectionMethod;
use SplFileObject;

abstract class ModelReflectionHelper
{
    protected static $relTypes = [
        'hasMany',
        'hasManyThrough',
        'belongsToMany',
        'hasOne',
        'belongsTo',
        'morphOne',
        'morphTo',
        'morphMany',
        'morphToMany',
        'morphedByMany'
    ];

    /**
     * @param  ReflectionMethod $method
     * @return string
     */
    public static function getCodeForMethod(ReflectionMethod $method): string
    {
        $fileName = $method->getFileName();

        $file = new SplFileObject($fileName);
        $file->seek($method->getStartLine() - 1);
        $code = '';
        while ($file->key() < $method->getEndLine()) {
            $code .= $file->current();
            $file->next();
        }

        $code  = trim(preg_replace('/\s\s+/', '', $code));
        $begin = strpos($code, 'function(');
        $code  = substr($code, $begin, strrpos($code, '}') - $begin + 1);
        return $code;
    }

    /**
     * @param  Model $model
     * @return array
     */
    public static function getModelClassMethods(Model $model): array
    {
        // TODO: Handle case when Mock::class not present
        return array_diff(
            get_class_methods($model),
            get_class_methods(Model::class),
            get_class_methods(Mock::class),
            get_class_methods(MetadataTrait::class)
        );
    }

    /**
     * @param  Model               $model
     * @throws ReflectionException
     * @return array|string[]
     */
    public static function getRelationshipsFromMethods(Model $model): array
    {
        $relationships = [];
        $methods       = self::getModelClassMethods($model);
        foreach ($methods as $method) {
            //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php<commit_msg>Added a cast bool to int (false to 0)<commit_after><?php declare(strict_types=1);


namespace AlgoWeb\PODataLaravel\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Mockery\Mock;
use POData\Common\InvalidOperationException;
use ReflectionException;
use ReflectionMethod;
use SplFileObject;

abstract class ModelReflectionHelper
{
    protected static $relTypes = [
        'hasMany',
        'hasManyThrough',
        'belongsToMany',
        'hasOne',
        'belongsTo',
        'morphOne',
        'morphTo',
        'morphMany',
        'morphToMany',
        'morphedByMany'
    ];

    /**
     * @param  ReflectionMethod $method
     * @return string
     */
    public static function getCodeForMethod(ReflectionMethod $method): string
    {
        $fileName = $method->getFileName();

        $file = new SplFileObject($fileName);
        $file->seek($method->getStartLine() - 1);
        $code = '';
        while ($file->key() < $method->getEndLine()) {
            $code .= $file->current();
            $file->next();
        }

        $code  = trim(preg_replace('/\s\s+/', '', $code));
        $begin = strpos($code, 'function(');
        $code  = substr($code, (int)$begin, strrpos($code, '}') - $begin + 1);
        return $code;
    }

    /**
     * @param  Model $model
     * @return array
     */
    public static function getModelClassMethods(Model $model): array
    {
        // TODO: Handle case when Mock::class not present
        return array_diff(
            get_class_methods($model),
            get_class_methods(Model::class),
            get_class_methods(Mock::class),
            get_class_methods(MetadataTrait::class)
        );
    }

    /**
     * @param  Model               $model
     * @throws ReflectionException
     * @return array|string[]
     */
    public static function getRelationshipsFromMethods(Model $model): array
    {
        $relationships = [];
        $methods       = self::getModelClassMethods($model);
        foreach ($methods as $method) {
            //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
{'fraction_non_alphanumeric': 0.12931633835457707, 'fraction_numerical': 0.0016222479721900347, 'mean_word_length': 2.369242779078845, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4135455', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1231, 'n_words': 259}