<commit_before><?php

namespace AppBundle\Controller;

use Abraham\TwitterOAuth\TwitterOAuth;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TwitterController extends Controller {
	const SESSION_TWITTER_AUTH = 'twitter_auth';
	const COOKIE_TWITTER_TOKEN = 'twitter_auth';

	/**
	 * @Route("/twitterAuthRedirect", name="twitterAuthRedirect")
	 * @Method("GET")
	 *
	 * @param Request $request
	 *
	 * @return RedirectResponse
	 */
	public function twitterAuthRedirectAction(Request $request){
		$connection = $this->getTwitter();

		$return_url = $this->generateAbsoluteUrl('twitterAuthReturn');

		$twitter_request_token = $connection->oauth('oauth/request_token', ['oauth_callback' => $return_url]);

		$twitter_auth_url = $connection->url('oauth/authorize', ['oauth_token' => $twitter_request_token['oauth_token'],]);

		$request->getSession()->set(self::SESSION_TWITTER_AUTH, $twitter_request_token['oauth_token_secret']);

		return $this->redirect($twitter_auth_url);
	}

	/**
	 * @Route("/twitterAuthReturn", name="twitterAuthReturn")
	 * @Method("GET")
	 *
	 * @param Request $request
	 *
	 * @return RedirectResponse
	 */
	public function twitterAuthReturnAction(Request $request){
		$query = $request->query;
		$twitter_token = $query->get('oauth_token');
		$twitter_token_secret = $request->getSession()->get(self::SESSION_TWITTER_AUTH);

		$connection = $this->getTwitter($twitter_token, $twitter_token_secret);
		$twitter_access_token = $connection->oauth('oauth/access_token', ['oauth_verifier' => $query->get('oauth_verifier')]);

		$file_path = $this->getFileName();

		file_put_contents($file_path, json_encode($twitter_access_token));

		$redirect_url = $this->generateAbsoluteUrl('twitterList');
		$response = new RedirectResponse($redirect_url);

		return $response;
	}

	/**
	 * @Route("/twitterList", name="twitterList")
	 * @Method("GET")
	 *
	 * @return Response
	 */
	public function twitterListAction(){
		$token_store = $this->getStoredToken();

		if (!empty($token_store)){
			$connection = $this->getTwitter($token_store['oauth_token'], $token_store['oauth_token_secret']);
			$statuses = $connection->get('statuses/home_timeline', ['exclude_replies' => true, 'count' => 100]);
			$statuses = $this->expandUrls($statuses);
		}
		else {
			$statuses = [];
		}

		$url_twitter_auth = $this->generateUrl('twitterAuthRedirect');
		$response = $this->render('AppBundle:Twitter:twitter_list.html.twig', [
			'tweets' => $statuses,
			'url_twitter_auth' => $url_twitter_auth,
			'access_token' => $token_store,
		]);

		return $response;
	}

	/**
	 * @param string $token
	 * @param string $secret
	 * @return TwitterOAuth
	 */
	private function getTwitter($token = null, $secret = null){
		$connection = new TwitterOAuth($this->getParameter('twitter_key'), $this->getParameter('twitter_secret'), $token, $secret);

		return $connection;
	}

	/**
	 * @param array $statuses
	 * @return array
	 */
	private function expandUrls(array $statuses){
		foreach ($statuses as &$tweet){
			$urls = $tweet->entities->urls;
			$parsed_urls = [];

			if (empty($urls)){
				$tweet->parsed_urls = $parsed_urls;
				continue;
			}

			foreach ($urls as $url){
				if ($this->containsIgnoredUrl($url->expanded_url)){
					continue;
				}

				$parsed_url = $this->expandShortUrl($url->expanded_url);

				if ($this->containsIgnoredUrl($parsed_url)){
					continue;
				}

				$parsed_urls[] = $parsed_url;
			}
			$tweet->parsed_urls = $parsed_urls;
		}

		return $statuses;
	}

	/**
	 * @param string $url
	 * @return bool
	 */
	private function containsIgnoredUrl($url){
		$ignored = [
			'twitter.com',
			't.co',
			'youtu.be',
			'www.youtube.com',
			'www.facebook.com',
			'facebook.com',
			'www.swarmapp.com',
		];
		$url = str_replace(['http://', 'https://'], '', $url);
		foreach ($ignored as $ignore){
			if (stripos($url, $ignore)===0){
				return true;
			}
		}

		return false;
	}

	/**
	 * @param string $url
	 * @return string
	 */
	private function expandShortUrl(string $url) : string {
		$headers = get_headers($url, 1);

		if (!empty($headers['Location'])){
			return is_array($headers['Location']) ? end($headers['Location']) : $headers['Location'];
		}

		return $url;
	}

	/**
	 * @param string $route
	 *
	 * @return string
	 */
	private function generateAbsoluteUrl($route){
		$url = $this->generateUrl($route);
		$url_absolute = $this->absoluteUrl($url);

		return $url_absolute;
	}
<commit_msg>Remove php 7 code<commit_after><?php

namespace AppBundle\Controller;

use Abraham\TwitterOAuth\TwitterOAuth;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class TwitterController extends Controller {
	const SESSION_TWITTER_AUTH = 'twitter_auth';
	const COOKIE_TWITTER_TOKEN = 'twitter_auth';

	/**
	 * @Route("/twitterAuthRedirect", name="twitterAuthRedirect")
	 * @Method("GET")
	 *
	 * @param Request $request
	 *
	 * @return RedirectResponse
	 */
	public function twitterAuthRedirectAction(Request $request){
		$connection = $this->getTwitter();

		$return_url = $this->generateAbsoluteUrl('twitterAuthReturn');

		$twitter_request_token = $connection->oauth('oauth/request_token', ['oauth_callback' => $return_url]);

		$twitter_auth_url = $connection->url('oauth/authorize', ['oauth_token' => $twitter_request_token['oauth_token'],]);

		$request->getSession()->set(self::SESSION_TWITTER_AUTH, $twitter_request_token['oauth_token_secret']);

		return $this->redirect($twitter_auth_url);
	}

	/**
	 * @Route("/twitterAuthReturn", name="twitterAuthReturn")
	 * @Method("GET")
	 *
	 * @param Request $request
	 *
	 * @return RedirectResponse
	 */
	public function twitterAuthReturnAction(Request $request){
		$query = $request->query;
		$twitter_token = $query->get('oauth_token');
		$twitter_token_secret = $request->getSession()->get(self::SESSION_TWITTER_AUTH);

		$connection = $this->getTwitter($twitter_token, $twitter_token_secret);
		$twitter_access_token = $connection->oauth('oauth/access_token', ['oauth_verifier' => $query->get('oauth_verifier')]);

		$file_path = $this->getFileName();

		file_put_contents($file_path, json_encode($twitter_access_token));

		$redirect_url = $this->generateAbsoluteUrl('twitterList');
		$response = new RedirectResponse($redirect_url);

		return $response;
	}

	/**
	 * @Route("/twitterList", name="twitterList")
	 * @Method("GET")
	 *
	 * @return Response
	 */
	public function twitterListAction(){
		$token_store = $this->getStoredToken();

		if (!empty($token_store)){
			$connection = $this->getTwitter($token_store['oauth_token'], $token_store['oauth_token_secret']);
			$statuses = $connection->get('statuses/home_timeline', ['exclude_replies' => true, 'count' => 100]);
			$statuses = $this->expandUrls($statuses);
		}
		else {
			$statuses = [];
		}

		$url_twitter_auth = $this->generateUrl('twitterAuthRedirect');
		$response = $this->render('AppBundle:Twitter:twitter_list.html.twig', [
			'tweets' => $statuses,
			'url_twitter_auth' => $url_twitter_auth,
			'access_token' => $token_store,
		]);

		return $response;
	}

	/**
	 * @param string $token
	 * @param string $secret
	 * @return TwitterOAuth
	 */
	private function getTwitter($token = null, $secret = null){
		$connection = new TwitterOAuth($this->getParameter('twitter_key'), $this->getParameter('twitter_secret'), $token, $secret);

		return $connection;
	}

	/**
	 * @param array $statuses
	 * @return array
	 */
	private function expandUrls(array $statuses){
		foreach ($statuses as &$tweet){
			$urls = $tweet->entities->urls;
			$parsed_urls = [];

			if (empty($urls)){
				$tweet->parsed_urls = $parsed_urls;
				continue;
			}

			foreach ($urls as $url){
				if ($this->containsIgnoredUrl($url->expanded_url)){
					continue;
				}

				$parsed_url = $this->expandShortUrl($url->expanded_url);

				if ($this->containsIgnoredUrl($parsed_url)){
					continue;
				}

				$parsed_urls[] = $parsed_url;
			}
			$tweet->parsed_urls = $parsed_urls;
		}

		return $statuses;
	}

	/**
	 * @param string $url
	 * @return bool
	 */
	private function containsIgnoredUrl($url){
		$ignored = [
			'twitter.com',
			't.co',
			'youtu.be',
			'www.youtube.com',
			'www.facebook.com',
			'facebook.com',
			'www.swarmapp.com',
		];
		$url = str_replace(['http://', 'https://'], '', $url);
		foreach ($ignored as $ignore){
			if (stripos($url, $ignore)===0){
				return true;
			}
		}

		return false;
	}

	/**
	 * @param string $url
	 * @return string
	 */
	private function expandShortUrl($url){
		$headers = get_headers($url, 1);

		if (!empty($headers['Location'])){
			return is_array($headers['Location']) ? end($headers['Location']) : $headers['Location'];
		}

		return $url;
	}

	/**
	 * @param string $route
	 *
	 * @return string
	 */
	private function generateAbsoluteUrl($route){
		$url = $this->generateUrl($route);
		$url_absolute = $this->absoluteUrl($url);

		return $url_absolute;
	}

{'fraction_non_alphanumeric': 0.16792352628783855, 'fraction_numerical': 0.001168348380244291, 'mean_word_length': 4.826732673267327, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 89, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, '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': '3623113', 'n_tokens_mistral': 3433, 'n_tokens_neox': 3048, 'n_words': 511}