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

namespace Rector\Builder;

use PhpParser\BuilderFactory;
use PhpParser\Node\Expr;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Builder\Class_\VariableInfo;
use Rector\Node\NodeFactory;

final class ConstructorMethodBuilder
{
    /**
     * @var BuilderFactory
     */
    private $builderFactory;

    /**
     * @var StatementGlue
     */
    private $statementGlue;

    /**
     * @var NodeFactory
     */
    private $nodeFactory;

    public function __construct(BuilderFactory $builderFactory, StatementGlue $statementGlue, NodeFactory $nodeFactory)
    {
        $this->builderFactory = $builderFactory;
        $this->statementGlue = $statementGlue;
        $this->nodeFactory = $nodeFactory;
    }

    public function addPropertyAssignWithExpression(
        Class_ $classNode,
        VariableInfo $parameterVariableInfo,
        Expr $exprNode,
        VariableInfo $propertyVariableInfo
    ): void {
        $propertyAssignNode = $this->nodeFactory->createPropertyAssignmentWithExpr(
            $propertyVariableInfo->getName(),
            $exprNode
        );

        $this->addVariableAndAssignToConstructorArgumentsOfClass($classNode, $parameterVariableInfo, $propertyAssignNode);
    }

    public function addPropertyAssignToClass(Class_ $classNode, VariableInfo $variableInfo): void
    {
        $propertyAssignNode = $this->nodeFactory->createPropertyAssignment($variableInfo->getName());
        $this->addVariableAndAssignToConstructorArgumentsOfClass($classNode, $variableInfo, $propertyAssignNode);
    }

    private function addVariableAndAssignToConstructorArgumentsOfClass(
        Class_ $classNode,
        VariableInfo $variableInfo,
        Expression $assignNode
    ): void {
        $constructorMethod = $classNode->getMethod('__construct');
        /** @var ClassMethod $constructorMethod */
        if ($constructorMethod) {
            $this->addVariableAssignToMethod($variableInfo, $constructorMethod, $assignNode);
            return;
        }

        $constructorMethod = $this->createClassMethodNodeWithParameterAndAssign($variableInfo, $assignNode);
        $this->statementGlue->addAsFirstMethod($classNode, $constructorMethod);
    }

    private function createClassMethodNodeWithParameterAndAssign(
        VariableInfo $variableInfo,
        Expression $expressionNode
    ): ClassMethod {<commit_msg>fix cs<commit_after><?php declare(strict_types=1);

namespace Rector\Builder;

use PhpParser\BuilderFactory;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Builder\Class_\VariableInfo;
use Rector\Node\NodeFactory;

final class ConstructorMethodBuilder
{
    /**
     * @var BuilderFactory
     */
    private $builderFactory;

    /**
     * @var StatementGlue
     */
    private $statementGlue;

    /**
     * @var NodeFactory
     */
    private $nodeFactory;

    public function __construct(BuilderFactory $builderFactory, StatementGlue $statementGlue, NodeFactory $nodeFactory)
    {
        $this->builderFactory = $builderFactory;
        $this->statementGlue = $statementGlue;
        $this->nodeFactory = $nodeFactory;
    }

    public function addPropertyAssignWithExpression(
        Class_ $classNode,
        VariableInfo $parameterVariableInfo,
        Expr $exprNode,
        VariableInfo $propertyVariableInfo
    ): void {
        $propertyAssignNode = $this->nodeFactory->createPropertyAssignmentWithExpr(
            $propertyVariableInfo->getName(),
            $exprNode
        );

        $this->addVariableAndAssignToConstructorArgumentsOfClass(
            $classNode,
            $parameterVariableInfo,
            $propertyAssignNode
        );
    }

    public function addPropertyAssignToClass(Class_ $classNode, VariableInfo $variableInfo): void
    {
        $propertyAssignNode = $this->nodeFactory->createPropertyAssignment($variableInfo->getName());
        $this->addVariableAndAssignToConstructorArgumentsOfClass($classNode, $variableInfo, $propertyAssignNode);
    }

    private function addVariableAndAssignToConstructorArgumentsOfClass(
        Class_ $classNode,
        VariableInfo $variableInfo,
        Expression $assignNode
    ): void {
        $constructorMethod = $classNode->getMethod('__construct');
        /** @var ClassMethod $constructorMethod */
        if ($constructorMethod) {
            $this->addVariableAssignToMethod($variableInfo, $constructorMethod, $assignNode);
            return;
        }

        $constructorMethod = $this->createClassMethodNodeWithParameterAndAssign($variableInfo, $assignNode);
        $this->statementGlue->addAsFirstMethod($classNode, $constructorMethod);
    }

    private function createClassMethodNodeWithParameterAndAssign(
        VariableInfo $variableInfo,
        Expression $expressionNode
    ): ClassMethod {
{'fraction_non_alphanumeric': 0.09511465603190429, 'fraction_numerical': 0.00039880358923230307, 'mean_word_length': 3.4232804232804233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '7646698', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1284, 'n_words': 228}