<commit_before><?php

/*
 * Copyright 2011 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace JMS\SerializerBundle\Serializer;

use JMS\SerializerBundle\Metadata\ClassMetadata;
use Metadata\MetadataFactoryInterface;
use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;
use JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface;

final class GraphNavigator
{
    private $exclusionStrategy;
    private $metadataFactory;
    private $visiting;

    public function __construct(MetadataFactoryInterface $metadataFactory, ExclusionStrategyInterface $exclusionStrategy = null)
    {
        $this->metadataFactory = $metadataFactory;
        $this->exclusionStrategy = $exclusionStrategy;
        $this->visiting = new \SplObjectStorage();
    }

    public function accept($data, $type, VisitorInterface $visitor)
    {
        // determine type if not given
        if ($isSerialization = (null === $type)) {
            if (null === $data) {
                return null;
            }

            $type = gettype($data);
            if ('object' === $type) {
                $type = get_class($data);
            }
        }

        if ('string' === $type) {
            return $visitor->visitString($data, $type);
        } else if ('integer' === $type) {
            return $visitor->visitInteger($data, $type);
        } else if ('boolean' === $type) {
            return $visitor->visitBoolean($data, $type);
        } else if ('double' === $type) {
            return $visitor->visitDouble($data, $type);
        } else if ('array' === $type || 0 === strpos($type, 'array<')) {
            return $visitor->visitArray($data, $type);
        } else {
            if ($isSerialization && null !== $data) {
                if ($this->visiting->contains($data)) {
                    return null;
                }
                $this->visiting->attach($data);
            }

            $handled = false;

            if ($isSerialization) {
                if ($data instanceof SerializationHandlerInterface) {
                    $rs = $data->serialize($visitor, $data, $type, $handled);
                }
            } elseif (in_array('JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface', class_implements($type))) {
                $handler = new $type;
                $rs = $handler->deserialize($visitor, $data, $type, $handled);
            }

            if ($handled) {
                return $rs;
            }

             // try custom handler
            $rs = $visitor->visitUsingCustomHandler($data, $type, $handled);
            if ($handled) {
                if ($isSerialization) {
                    $this->visiting->detach($data);
                }

                return $rs;
            }

            $metadata = $this->metadataFactory->getMetadataForClass($type);
            if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata)) {
                if ($isSerialization) {
                    $this->visiting->detach($data);
                }

                return null;
            }

            // pre-serialization callbacks
            if ($isSerialization) {
                foreach ($metadata->preSerializeMethods as $method) {<commit_msg>cosmetic tweak<commit_after><?php

/*
 * Copyright 2011 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace JMS\SerializerBundle\Serializer;

use JMS\SerializerBundle\Metadata\ClassMetadata;
use Metadata\MetadataFactoryInterface;
use JMS\SerializerBundle\Serializer\Exclusion\ExclusionStrategyInterface;
use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;

final class GraphNavigator
{
    private $exclusionStrategy;
    private $metadataFactory;
    private $visiting;

    public function __construct(MetadataFactoryInterface $metadataFactory, ExclusionStrategyInterface $exclusionStrategy = null)
    {
        $this->metadataFactory = $metadataFactory;
        $this->exclusionStrategy = $exclusionStrategy;
        $this->visiting = new \SplObjectStorage();
    }

    public function accept($data, $type, VisitorInterface $visitor)
    {
        // determine type if not given
        if ($isSerialization = (null === $type)) {
            if (null === $data) {
                return null;
            }

            $type = gettype($data);
            if ('object' === $type) {
                $type = get_class($data);
            }
        }

        if ('string' === $type) {
            return $visitor->visitString($data, $type);
        } else if ('integer' === $type) {
            return $visitor->visitInteger($data, $type);
        } else if ('boolean' === $type) {
            return $visitor->visitBoolean($data, $type);
        } else if ('double' === $type) {
            return $visitor->visitDouble($data, $type);
        } else if ('array' === $type || 0 === strpos($type, 'array<')) {
            return $visitor->visitArray($data, $type);
        } else {
            if ($isSerialization && null !== $data) {
                if ($this->visiting->contains($data)) {
                    return null;
                }
                $this->visiting->attach($data);
            }

            $handled = false;

            if ($isSerialization) {
                if ($data instanceof SerializationHandlerInterface) {
                    $rs = $data->serialize($visitor, $data, $type, $handled);
                }
            } elseif (in_array('JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface', class_implements($type))) {
                $handler = new $type;
                $rs = $handler->deserialize($visitor, $data, $type, $handled);
            }

            if (!$handled) {
                 // try custom handler
                $rs = $visitor->visitUsingCustomHandler($data, $type, $handled);
            }

            if ($handled) {
                if ($isSerialization) {
                    $this->visiting->detach($data);
                }
                return $rs;
            }

            $metadata = $this->metadataFactory->getMetadataForClass($type);
            if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata)) {
                if ($isSerialization) {
                    $this->visiting->detach($data);
                }

                return null;
            }

            // pre-serialization callbacks
            if ($isSerialization) {
                foreach ($metadata->preSerializeMethods as $method) {
{'fraction_non_alphanumeric': 0.12171651495448635, 'fraction_numerical': 0.0023407022106631988, 'mean_word_length': 2.1099878689850384, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '943326', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1928, 'n_words': 503}