<commit_before>import ImporterInterface from "./ImporterInterface";
import PhpParser from 'php-parser';
import { SerializedConfigurationInterface } from "../Configuration";

export default class PhpImporter implements ImporterInterface {

    readonly handle: string = 'php';

    readonly name: string = 'PHP';

    readonly pastePlaceholder: string = 'Paste here the full contents of your .php_cs / .php_cs.dist files (or just the array with the rules).';

    private readonly parser: any;

    public constructor() {
        this.parser = new PhpParser({
            parser: {
                debug: false,
                locations: false,
                extractDoc: false,
                suppressErrors: false
            },
            lexer: {
                all_tokens: false,
                comment_tokens: false,
                mode_eval: true,
                asp_tags: false,
                short_tags: false
            }
        });
    }

    parse(serialized: string): SerializedConfigurationInterface {
        let ast = this.getAST(serialized),
            setRulesArray = this.findRelevantValue(ast, 'setRules');
        if (setRulesArray === null) {
            throw new Error('Unable to find the setRules() call, and the PHP code is not an array.');
        }
        let configuration = this.parseSetRulesArray(setRulesArray);
        let indentValue = this.findRelevantValue(ast, 'setIndent');
        if (typeof indentValue === 'string') {
            if (configuration.whitespace === undefined) {
                configuration.whitespace = {};
            }
            configuration.whitespace.indent = indentValue;
        }<commit_msg>PHP Importer: add <?php if it's missing<commit_after>import ImporterInterface from "./ImporterInterface";
import PhpParser from 'php-parser';
import { SerializedConfigurationInterface } from "../Configuration";

export default class PhpImporter implements ImporterInterface {

    readonly handle: string = 'php';

    readonly name: string = 'PHP';

    readonly pastePlaceholder: string = 'Paste here the full contents of your .php_cs / .php_cs.dist files (or just the array with the rules).';

    private readonly parser: any;

    public constructor() {
        this.parser = new PhpParser({
            parser: {
                debug: false,
                locations: false,
                extractDoc: false,
                suppressErrors: false
            },
            lexer: {
                all_tokens: false,
                comment_tokens: false,
                mode_eval: true,
                asp_tags: false,
                short_tags: false
            }
        });
    }

    parse(serialized: string): SerializedConfigurationInterface {
        let ast;
        try {
            ast = this.getAST(serialized);
        } catch (e) {
            try {
                ast = this.getAST(`<?php\n${serialized}`);
            } catch (e2) {
                throw e;
            }
        }
        let setRulesArray = this.findRelevantValue(ast, 'setRules');
        if (setRulesArray === null) {
            throw new Error('Unable to find the setRules() call, and the PHP code is not an array.');
        }
        let configuration = this.parseSetRulesArray(setRulesArray);
        let indentValue = this.findRelevantValue(ast, 'setIndent');
        if (typeof indentValue === 'string') {
            if (configuration.whitespace === undefined) {
                configuration.whitespace = {};
            }
            configuration.whitespace.indent = indentValue;
        }
{'fraction_non_alphanumeric': 0.09373251259093453, 'fraction_numerical': 0.0002797985450475658, 'mean_word_length': 1.9207516339869282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4408690', 'n_tokens_mistral': 911, 'n_tokens_neox': 873, 'n_words': 258}